Commit bf3ea2f8efbcf438de53733a0daf4e8e92de2b36
1 parent
1b51eed04d
Exists in
master
format number
Showing
2 changed files
with
30 additions
and
0 deletions
Show diff stats
float_util.go
... | ... | @@ -4,6 +4,7 @@ import ( |
4 | 4 | "fmt" |
5 | 5 | "math" |
6 | 6 | "math/big" |
7 | + "strings" | |
7 | 8 | ) |
8 | 9 | |
9 | 10 | func RoundFloat64(f float64, dec int) float64 { |
... | ... | @@ -64,3 +65,20 @@ func Float64PtrToString(f *float64) string { |
64 | 65 | } |
65 | 66 | return fmt.Sprintf("%.2f", *f) |
66 | 67 | } |
68 | + | |
69 | +func FormatFloat64Number(f float64, dec int) string { | |
70 | + res := "" | |
71 | + | |
72 | + f = RoundFloat64(f, dec) | |
73 | + | |
74 | + i := int64(f) | |
75 | + | |
76 | + format := fmt.Sprintf("%%.%df", dec) | |
77 | + parts := strings.Split(fmt.Sprintf(format, f-float64(i)), ".") | |
78 | + | |
79 | + decimals := parts[1] | |
80 | + | |
81 | + res = FormatInt64Number(i) + "," + decimals | |
82 | + | |
83 | + return res | |
84 | +} | ... | ... |
int_util.go
... | ... | @@ -65,3 +65,15 @@ func MinInt64(vars ...int64) (min int64) { |
65 | 65 | } |
66 | 66 | return min |
67 | 67 | } |
68 | + | |
69 | +func FormatInt64Number(i int64) string { | |
70 | + res := "" | |
71 | + | |
72 | + for i >= 1000 { | |
73 | + rem := i % 1000 | |
74 | + i = i / 1000 | |
75 | + res = res + fmt.Sprintf(".%03d", rem) | |
76 | + } | |
77 | + res = fmt.Sprintf("%d", i) + res | |
78 | + return res | |
79 | +} | ... | ... |