-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
handle big numbers and logfile in dockerfile
- Loading branch information
Showing
8 changed files
with
211 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,4 @@ new: build | |
@go build -o bin/$(BINARY_NAME) && ./bin/$(BINARY_NAME) | ||
|
||
test: | ||
@go test -v ./... | ||
@go test -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package main | ||
|
||
import ( | ||
"math" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// credit to https://github.com/DeyV/gotools/blob/master/numbers.go | ||
func roundPrec(x float64, prec int) float64 { | ||
if math.IsNaN(x) || math.IsInf(x, 0) { | ||
return x | ||
} | ||
|
||
sign := 1.0 | ||
if x < 0 { | ||
sign = -1 | ||
x *= -1 | ||
} | ||
|
||
var rounder float64 | ||
pow := math.Pow(10, float64(prec)) | ||
intermed := x * pow | ||
_, frac := math.Modf(intermed) | ||
|
||
if frac >= 0.5 { | ||
rounder = math.Ceil(intermed) | ||
} else { | ||
rounder = math.Floor(intermed) | ||
} | ||
|
||
return rounder / pow * sign | ||
} | ||
|
||
func numberFormat(number float64, decimals int, decPoint, thousandsSep string) string { | ||
if math.IsNaN(number) || math.IsInf(number, 0) { | ||
number = 0 | ||
} | ||
|
||
var ret string | ||
var negative bool | ||
|
||
if number < 0 { | ||
number *= -1 | ||
negative = true | ||
} | ||
|
||
d, fract := math.Modf(number) | ||
|
||
if decimals <= 0 { | ||
fract = 0 | ||
} else { | ||
pow := math.Pow(10, float64(decimals)) | ||
fract = roundPrec(fract*pow, 0) | ||
} | ||
|
||
if thousandsSep == "" { | ||
ret = strconv.FormatFloat(d, 'f', 0, 64) | ||
} else if d >= 1 { | ||
var x float64 | ||
for d >= 1 { | ||
d, x = math.Modf(d / 1000) | ||
x = x * 1000 | ||
ret = strconv.FormatFloat(x, 'f', 0, 64) + ret | ||
if d >= 1 { | ||
ret = thousandsSep + ret | ||
} | ||
} | ||
} else { | ||
ret = "0" | ||
} | ||
|
||
fracts := strconv.FormatFloat(fract, 'f', 0, 64) | ||
|
||
// "0" pad left | ||
for i := len(fracts); i < decimals; i++ { | ||
fracts = "0" + fracts | ||
} | ||
|
||
ret += decPoint + fracts | ||
|
||
if negative { | ||
ret = "-" + ret | ||
} | ||
return ret | ||
} | ||
|
||
func roundInt(input float64) int { | ||
var result float64 | ||
|
||
if input < 0 { | ||
result = math.Ceil(input - 0.5) | ||
} else { | ||
result = math.Floor(input + 0.5) | ||
} | ||
|
||
// only interested in integer, ignore fractional | ||
i, _ := math.Modf(result) | ||
|
||
return int(i) | ||
} | ||
|
||
func formatNumber(input float64) string { | ||
x := roundInt(input) | ||
xFormatted := numberFormat(float64(x), 2, ".", ",") | ||
return xFormatted | ||
} | ||
|
||
func nearestThousandFormat(num float64) string { | ||
|
||
if math.Abs(num) < 999.5 { | ||
xNum := formatNumber(num) | ||
xNumStr := xNum[:len(xNum)-3] | ||
return string(xNumStr) | ||
} | ||
|
||
xNum := formatNumber(num) | ||
// first, remove the .00 then convert to slice | ||
xNumStr := xNum[:len(xNum)-3] | ||
xNumCleaned := strings.Replace(xNumStr, ",", " ", -1) | ||
xNumSlice := strings.Fields(xNumCleaned) | ||
count := len(xNumSlice) - 2 | ||
unit := [4]string{"k", "m", "b", "t"} | ||
xPart := unit[count] | ||
|
||
afterDecimal := "" | ||
if xNumSlice[1][0] != 0 { | ||
afterDecimal = "." + string(xNumSlice[1][0]) | ||
} | ||
final := xNumSlice[0] + afterDecimal + xPart | ||
return final | ||
} |