Skip to content

Instantly share code, notes, and snippets.

@TechnotronicOz
Forked from pelegm/round.go
Last active August 29, 2015 14:17
Show Gist options
  • Save TechnotronicOz/d6add11cf637a27f73a7 to your computer and use it in GitHub Desktop.
Save TechnotronicOz/d6add11cf637a27f73a7 to your computer and use it in GitHub Desktop.
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