/
float.go
52 lines (44 loc) · 1.11 KB
/
float.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package helper
import (
"math"
)
// RoundUp rounding up ex: 1.5 -> 2
func RoundUp(v float64, precision int) float64 {
return math.Ceil(v*(math.Pow10(precision))) / math.Pow10(precision)
}
// RoundDown rounding down ex: 1.5 -> 1
func RoundDown(v float64, precision int) float64 {
return math.Floor(v*(math.Pow10(precision))) / math.Pow10(precision)
}
// Round rounding by value ex: 1.3 -> 1 or 1.6 -> 2
func Round(v float64, precision int) float64 {
return math.Round(v*(math.Pow10(precision))) / math.Pow10(precision)
}
// MinFloat32 min float, if v is greater than min, min will prevail
func MinFloat32(v, min float32) float32 {
if v < min {
return min
}
return v
}
// MaxFloat32 max float, if v is less than max, max will prevail
func MaxFloat32(v, max float32) float32 {
if v > max {
return max
}
return v
}
// MinFloat64 min float, if v is greater than min, min will prevail
func MinFloat64(v, min float64) float64 {
if v < min {
return min
}
return v
}
// MaxFloat64 max float, if v is less than max, max will prevail
func MaxFloat64(v, max float64) float64 {
if v > max {
return max
}
return v
}