forked from massivebox/ecodash
Rewrite the rounding alogithm
This commit is contained in:
parent
9023806d57
commit
66e2a2de1a
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
|||
config.json
|
||||
cache.json
|
||||
go.sum
|
||||
go.sum
|
||||
.idea
|
8
.idea/.gitignore
vendored
8
.idea/.gitignore
vendored
|
@ -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
|
|
@ -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>
|
|
@ -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>
|
|
@ -1,8 +0,0 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="SwUserDefinedSpecifications">
|
||||
<option name="specTypeByUrl">
|
||||
<map />
|
||||
</option>
|
||||
</component>
|
||||
</project>
|
|
@ -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
31
http.go
|
@ -9,6 +9,7 @@ import (
|
|||
"math"
|
||||
"os"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -195,25 +196,15 @@ func (config Config) renderIndex(c *fiber.Ctx) error {
|
|||
}
|
||||
|
||||
func templateDivide(num1, num2 float32) template.HTML {
|
||||
var (
|
||||
isInDecimals bool
|
||||
hasHitMeaningful bool
|
||||
meaningfulDigits int
|
||||
rounded []byte
|
||||
num = num1 / num2
|
||||
)
|
||||
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
|
||||
}
|
||||
|
||||
division := float64(num1 / num2)
|
||||
|
||||
powerOfTen := int(math.Floor(math.Log10(division)))
|
||||
if powerOfTen >= -2 && powerOfTen <= 2 {
|
||||
return template.HTML(fmt.Sprintf("%s", strconv.FormatFloat(math.Round(division*100)/100, 'f', -1, 64)))
|
||||
}
|
||||
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))
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue