Works so long as you like rounding down when f is negative...
Is there any similar function to "round" in JavaScript?
To return the value of a number rounded to the nearest integer.
Is there any similar function to "round" in JavaScript?
To return the value of a number rounded to the nearest integer.
func round(val float64, prec int) float64 {var rounder float64
intermed := val * math.Pow(10, float64(prec))
if val >= 0.5 {
rounder = math.Ceil(intermed)} else {rounder = math.Floor(intermed)}
return rounder / math.Pow(10, float64(prec))}
@Gerard missed the precision
@Gerard missed the precisionHere is a combination of the solutions by Gerard and Jan Merci:
PS: Anyone know for definite why Round(123.424999999999993, 2) gets wrong result?
On Tuesday, 16 October 2012 21:32:59 UTC+2, Gerard wrote:
Op zondag 1 januari 2012 00:02:34 UTC+1 schreef Archos het volgende:Is there any similar function to "round" in JavaScript?
To return the value of a number rounded to the nearest integer.
// round value - convert to int64func Round(value float64) int64 {if value < 0.0 {value -= 0.5} else {value += 0.5}return int64(value)}
--
But also less correct.
Hope so too.
http://play.golang.org/p/S654PxAe_N fixes the problem for the other function as well.
--