Skip to content

Instantly share code, notes, and snippets.

@pelegm pelegm/round.go forked from DavidVaini/round.go
Last active Jan 5, 2018

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))
}
@GregIngelmo

This comment has been minimized.

Copy link

commented Feb 8, 2015

Just what I needed :) thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.