Rewrite the rounding alogithm

This commit is contained in:
MassiveBox 2022-11-13 19:53:32 +01:00
parent 9023806d57
commit 66e2a2de1a
7 changed files with 13 additions and 90 deletions

3
.gitignore vendored
View file

@ -1,3 +1,4 @@
config.json config.json
cache.json cache.json
go.sum go.sum
.idea

8
.idea/.gitignore vendored
View file

@ -1,8 +0,0 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View file

@ -1,9 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View file

@ -1,36 +0,0 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="HttpUrlsUsage" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="ignoredUrls">
<list>
<option value="http://localhost" />
<option value="http://127.0.0.1" />
<option value="http://0.0.0.0" />
<option value="http://www.w3.org/" />
<option value="http://json-schema.org/draft" />
<option value="http://java.sun.com/" />
<option value="http://xmlns.jcp.org/" />
<option value="http://javafx.com/javafx/" />
<option value="http://javafx.com/fxml" />
<option value="http://maven.apache.org/xsd/" />
<option value="http://maven.apache.org/POM/" />
<option value="http://www.springframework.org/schema/" />
<option value="http://www.springframework.org/tags" />
<option value="http://www.springframework.org/security/tags" />
<option value="http://www.thymeleaf.org" />
<option value="http://www.jboss.org/j2ee/schema/" />
<option value="http://www.jboss.com/xml/ns/" />
<option value="http://www.ibm.com/webservices/xsd" />
<option value="http://activemq.apache.org/schema/" />
<option value="http://schema.cloudfoundry.org/spring/" />
<option value="http://schemas.xmlsoap.org/" />
<option value="http://cxf.apache.org/schemas/" />
<option value="http://primefaces.org/ui" />
<option value="http://tiles.apache.org/" />
<option value="http://" />
</list>
</option>
</inspection_tool>
</profile>
</component>

View file

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SwUserDefinedSpecifications">
<option name="specTypeByUrl">
<map />
</option>
</component>
</project>

View file

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/ecodash.iml" filepath="$PROJECT_DIR$/.idea/ecodash.iml" />
</modules>
</component>
</project>

31
http.go
View file

@ -9,6 +9,7 @@ import (
"math" "math"
"os" "os"
"reflect" "reflect"
"strconv"
"time" "time"
) )
@ -195,25 +196,15 @@ func (config Config) renderIndex(c *fiber.Ctx) error {
} }
func templateDivide(num1, num2 float32) template.HTML { func templateDivide(num1, num2 float32) template.HTML {
var (
isInDecimals bool division := float64(num1 / num2)
hasHitMeaningful bool
meaningfulDigits int powerOfTen := int(math.Floor(math.Log10(division)))
rounded []byte if powerOfTen >= -2 && powerOfTen <= 2 {
num = num1 / num2 return template.HTML(fmt.Sprintf("%s", strconv.FormatFloat(math.Round(division*100)/100, 'f', -1, 64)))
)
for _, char := range []byte(fmt.Sprintf("%v", num)) {
if (isInDecimals && char != '0') || hasHitMeaningful {
hasHitMeaningful = true
meaningfulDigits++
}
if char == '.' {
isInDecimals = true
}
rounded = append(rounded, char)
if meaningfulDigits == 3 {
break
}
} }
return template.HTML(rounded)
preComma := division / math.Pow10(powerOfTen)
return template.HTML(fmt.Sprintf("%s * 10<sup>%d</sup>", strconv.FormatFloat(math.Round(preComma*100)/100, 'f', -1, 64), powerOfTen))
} }