forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathround.go
More file actions
28 lines (22 loc) · 703 Bytes
/
round.go
File metadata and controls
28 lines (22 loc) · 703 Bytes
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
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
}