diff --git a/.gitignore b/.gitignore index 76cffc0..64ca34e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ config.json cache.json -go.sum \ No newline at end of file +go.sum +.idea \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -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 diff --git a/.idea/ecodash.iml b/.idea/ecodash.iml deleted file mode 100644 index 5e764c4..0000000 --- a/.idea/ecodash.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml deleted file mode 100644 index 3018bff..0000000 --- a/.idea/inspectionProfiles/Project_Default.xml +++ /dev/null @@ -1,36 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 283b9b4..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 4f83220..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/http.go b/http.go index 3aa3768..2966753 100644 --- a/http.go +++ b/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%d", strconv.FormatFloat(math.Round(preComma*100)/100, 'f', -1, 64), powerOfTen)) + }