1
0
Fork 0
mirror of https://github.com/Luzifer/webcheck.git synced 2024-11-09 23:20:03 +00:00
webcheck/vendor/github.com/montanaflynn/stats/round.go
Knut Ahlers 2fbabd6bd4
Vendor dependencies
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2018-07-23 14:54:12 +02:00

38 lines
910 B
Go

package stats
import "math"
// Round a float to a specific decimal place or precision
func Round(input float64, places int) (rounded float64, err error) {
// If the float is not a number
if math.IsNaN(input) {
return math.NaN(), NaNErr
}
// Find out the actual sign and correct the input for later
sign := 1.0
if input < 0 {
sign = -1
input *= -1
}
// Use the places arg to get the amount of precision wanted
precision := math.Pow(10, float64(places))
// Find the decimal place we are looking to round
digit := input * precision
// Get the actual decimal number as a fraction to be compared
_, decimal := math.Modf(digit)
// If the decimal is less than .5 we round down otherwise up
if decimal >= 0.5 {
rounded = math.Ceil(digit)
} else {
rounded = math.Floor(digit)
}
// Finally we do the math to actually create a rounded number
return rounded / precision * sign, nil
}