Commit bf3ea2f8efbcf438de53733a0daf4e8e92de2b36

Authored by Marko Tikvić
1 parent 1b51eed04d
Exists in master

format number

Showing 2 changed files with 30 additions and 0 deletions   Show diff stats
1 package webutility 1 package webutility
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 "math" 5 "math"
6 "math/big" 6 "math/big"
7 "strings"
7 ) 8 )
8 9
9 func RoundFloat64(f float64, dec int) float64 { 10 func RoundFloat64(f float64, dec int) float64 {
10 p := math.Pow(10, float64(dec)) 11 p := math.Pow(10, float64(dec))
11 return math.Round(f*p) / p 12 return math.Round(f*p) / p
12 } 13 }
13 14
14 func NewBF(f float64, prec uint) *big.Float { 15 func NewBF(f float64, prec uint) *big.Float {
15 x := big.NewFloat(f) 16 x := big.NewFloat(f)
16 x.SetPrec(prec) 17 x.SetPrec(prec)
17 return x 18 return x
18 } 19 }
19 20
20 func AddBF(x, y *big.Float) *big.Float { 21 func AddBF(x, y *big.Float) *big.Float {
21 z := big.NewFloat(0.0) 22 z := big.NewFloat(0.0)
22 z.SetPrec(x.Prec()) 23 z.SetPrec(x.Prec())
23 z.Add(x, y) 24 z.Add(x, y)
24 return z 25 return z
25 } 26 }
26 27
27 func SubBF(x, y *big.Float) *big.Float { 28 func SubBF(x, y *big.Float) *big.Float {
28 z := big.NewFloat(0.0) 29 z := big.NewFloat(0.0)
29 z.SetPrec(x.Prec()) 30 z.SetPrec(x.Prec())
30 31
31 yneg := big.NewFloat(0.0) 32 yneg := big.NewFloat(0.0)
32 yneg.Neg(y) 33 yneg.Neg(y)
33 34
34 z.Add(x, yneg) 35 z.Add(x, yneg)
35 return z 36 return z
36 } 37 }
37 38
38 func MulBF(x, y *big.Float) *big.Float { 39 func MulBF(x, y *big.Float) *big.Float {
39 z := big.NewFloat(0.0) 40 z := big.NewFloat(0.0)
40 z.SetPrec(x.Prec()) 41 z.SetPrec(x.Prec())
41 z.Mul(x, y) 42 z.Mul(x, y)
42 return z 43 return z
43 } 44 }
44 45
45 func DivBF(x, y *big.Float) *big.Float { 46 func DivBF(x, y *big.Float) *big.Float {
46 z := big.NewFloat(0.0) 47 z := big.NewFloat(0.0)
47 z.SetPrec(x.Prec()) 48 z.SetPrec(x.Prec())
48 z.Quo(x, y) 49 z.Quo(x, y)
49 return z 50 return z
50 } 51 }
51 52
52 func BFtoFloat(f *big.Float) float64 { 53 func BFtoFloat(f *big.Float) float64 {
53 v, _ := f.Float64() 54 v, _ := f.Float64()
54 return v 55 return v
55 } 56 }
56 57
57 func Float64ToString(f float64) string { 58 func Float64ToString(f float64) string {
58 return fmt.Sprintf("%.2f", f) 59 return fmt.Sprintf("%.2f", f)
59 } 60 }
60 61
61 func Float64PtrToString(f *float64) string { 62 func Float64PtrToString(f *float64) string {
62 if f == nil { 63 if f == nil {
63 return "" 64 return ""
64 } 65 }
65 return fmt.Sprintf("%.2f", *f) 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 }
67 85
1 package webutility 1 package webutility
2 2
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 ) 5 )
6 6
7 // ClampInt64 ... 7 // ClampInt64 ...
8 func ClampInt64(v, min, max int64) int64 { 8 func ClampInt64(v, min, max int64) int64 {
9 if v < min { 9 if v < min {
10 return min 10 return min
11 } else if v > max { 11 } else if v > max {
12 return max 12 return max
13 } 13 }
14 14
15 return v 15 return v
16 } 16 }
17 17
18 // InRangeInt64 ... 18 // InRangeInt64 ...
19 func InRangeInt64(v, min, max int64) bool { 19 func InRangeInt64(v, min, max int64) bool {
20 return (v >= min && v <= max) 20 return (v >= min && v <= max)
21 } 21 }
22 22
23 // Int64ToString ... 23 // Int64ToString ...
24 func Int64ToString(i int64) string { 24 func Int64ToString(i int64) string {
25 return fmt.Sprintf("%d", i) 25 return fmt.Sprintf("%d", i)
26 } 26 }
27 27
28 // Int64PtrToString ... 28 // Int64PtrToString ...
29 func Int64PtrToString(i *int64) string { 29 func Int64PtrToString(i *int64) string {
30 if i == nil { 30 if i == nil {
31 return "" 31 return ""
32 } 32 }
33 return fmt.Sprintf("%d", *i) 33 return fmt.Sprintf("%d", *i)
34 } 34 }
35 35
36 // BoolToInt64 ... 36 // BoolToInt64 ...
37 func BoolToInt64(b bool) int64 { 37 func BoolToInt64(b bool) int64 {
38 if b { 38 if b {
39 return 1 39 return 1
40 } 40 }
41 return 0 41 return 0
42 } 42 }
43 43
44 // Int64ToBool ... 44 // Int64ToBool ...
45 func Int64ToBool(i int64) bool { 45 func Int64ToBool(i int64) bool {
46 return i != 0 46 return i != 0
47 } 47 }
48 48
49 func MaxInt(vars ...int) (max int) { 49 func MaxInt(vars ...int) (max int) {
50 max = vars[0] 50 max = vars[0]
51 for _, v := range vars { 51 for _, v := range vars {
52 if v > max { 52 if v > max {
53 max = v 53 max = v
54 } 54 }
55 } 55 }
56 return max 56 return max
57 } 57 }
58 58
59 func MinInt64(vars ...int64) (min int64) { 59 func MinInt64(vars ...int64) (min int64) {
60 min = vars[0] 60 min = vars[0]
61 for _, v := range vars { 61 for _, v := range vars {
62 if v < min { 62 if v < min {
63 min = v 63 min = v
64 } 64 }
65 } 65 }
66 return min 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 }
68 80