Skip to content

Instantly share code, notes, and snippets.

@yoander
Forked from pelegm/round.go
Created Dec 15, 2017
Embed
What would you like to do?
package main
import (
"log"
"math"
)
func Round(val float64, roundOn float64, places int ) (newVal float64) {
var round float64
pow := math.Pow(10, float64(places))
digit := pow * val
_, div := math.Modf(digit)
_div := math.Copysign(div, val)
_roundOn := math.Copysign(roundOn, val)
if _div >= _roundOn {
round = math.Ceil(digit)
} else {
round = math.Floor(digit)
}
newVal = round / pow
return
}
func main() {
log.Println(Round(7.503300000000001e-05, .5, 5))
log.Println(Round(-7.503300000000001e-05, .5, 5))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment