Skip to content

Instantly share code, notes, and snippets.

@indytechcook
Created June 4, 2013 14:38
Show Gist options
  • Save indytechcook/5706434 to your computer and use it in GitHub Desktop.
Save indytechcook/5706434 to your computer and use it in GitHub Desktop.
// return rounded version of x with prec precision.
func roundFloat(x float64, prec int) float64 {
var rounder float64
pow := math.Pow(10, float64(prec))
intermed := x * pow
_, frac := math.Modf(intermed)
intermed += .5
x = .5
if frac < 0.0 {
x = -.5
intermed -= 1
}
if frac >= x {
rounder = math.Ceil(intermed)
} else {
rounder = math.Floor(intermed)
}
return rounder / pow
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment