Created
June 4, 2013 14:38
-
-
Save indytechcook/5706434 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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