forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
round.go
43 lines (36 loc) · 1.21 KB
/
round.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
package mathx
import "math"
// Round rounds the "input" on "roundOn" (e.g. 0.5) on "places" digits.
func Round(input float64, roundOn float64, places float64) float64 {
pow := math.Pow(10, places)
digit := pow * input
_, div := math.Modf(digit)
if div >= roundOn {
return math.Ceil(digit) / pow
}
return math.Floor(digit) / pow
}
// RoundUp rounds up the "input" up to "places" digits.
func RoundUp(input float64, places float64) float64 {
pow := math.Pow(10, places)
return math.Ceil(pow*input) / pow
}
// RoundDown rounds down the "input" up to "places" digits.
func RoundDown(input float64, places float64) float64 {
pow := math.Pow(10, places)
return math.Floor(pow*input) / pow
}
// RoundToInteger rounds the given float64 to an integer.
func RoundToInteger(x float64) int {
// If the number is 1.1 round it to the previous integer (1), if >= 1.11 round it to the next one (2).
t := math.Trunc(x)
odd := math.Remainder(t, 2) != 0
d := math.Abs(x - t)
d = Round(d, 0.5, 2) // round to 2 decimals so we can easily check 0.11 and 0.1 and 0.2.
if d > 0.1 || (d == 0.2 && odd) {
// fmt.Printf("%f-%f -> %f. Is > 0.1 -> %v \n", x, t, d, d > 0.1)
t = t + math.Copysign(1, x)
return int(t)
}
return int(t)
}