FAQ
In this code is small bug, and it should look like that:

// Round return rounded version of x with prec precision.
func Round(x float64, prec int) float64 {
var rounder float64
pow := math.Pow(10, float64(prec))
intermed := x * pow
_, frac := math.Modf(intermed)

if frac >= 0.5 {
rounder = math.Ceil(intermed)
} else {
rounder = math.Floor(intermed)
}

return rounder / pow
}

Example: http://play.golang.org/p/6SnMqR9-9_

W dniu niedziela, 1 stycznia 2012 18:47:19 UTC+1 użytkownik Johann Höchtl
napisał:
This one should work:
...
--

## Search Discussions

•  at Oct 16, 2012 at 5:54 pm ⇧

On Tue, Oct 16, 2012 at 5:59 PM, Dawid Polak wrote:
In this code is small bug, and it should look like that:

// Round return rounded version of x with prec precision.
func Round(x float64, prec int) float64 {
var rounder float64
pow := math.Pow(10, float64(prec))
intermed := x * pow
_, frac := math.Modf(intermed)

if frac >= 0.5 {
rounder = math.Ceil(intermed)
} else {
rounder = math.Floor(intermed)
}

return rounder / pow
}

Example: http://play.golang.org/p/6SnMqR9-9_
http://play.golang.org/p/MlSteRZVX3

-j

--
•  at Oct 16, 2012 at 7:33 pm ⇧
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 int64
func Round(value float64) int64 {
if value < 0.0 {
value -= 0.5
} else {
value += 0.5
}
return int64(value)
}

--
•  at Oct 17, 2012 at 7:23 am ⇧
@Gerard missed the precision
Here is a combination of the solutions by Gerard and Jan Merci:

http://play.golang.org/p/ZmFfr07oHp

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 int64
func Round(value float64) int64 {
if value < 0.0 {
value -= 0.5
} else {
value += 0.5
}
return int64(value)
}
--
•  at Oct 17, 2012 at 10:33 am ⇧
Op woensdag 17 oktober 2012 09:23:29 UTC+2 schreef Rory McGuire het
volgende:
@Gerard missed the precision
No I didn't. The question was about rounding to the nearest integer.

Gerard

--
•  at Oct 18, 2012 at 9:38 am ⇧

On Wednesday, 17 October 2012 12:33:55 UTC+2, Gerard wrote:

Op woensdag 17 oktober 2012 09:23:29 UTC+2 schreef Rory McGuire het
volgende:
@Gerard missed the precision
No I didn't. The question was about rounding to the nearest integer.

Gerard

Because it is correct for positive and negative values AND it supports
multiple precisions. (Only up to float64's precision anyway).

Cheers,

--
•  at Oct 17, 2012 at 1:42 pm ⇧

On Wed, Oct 17, 2012 at 4:23 PM, Rory McGuire wrote:

@Gerard missed the precision
Here is a combination of the solutions by Gerard and Jan Merci:

http://play.golang.org/p/ZmFfr07oHp

PS: Anyone know for definite why Round(123.424999999999993, 2) gets wrong
result?
Float types have limited precisions, so you will inevitably get incorrect
results beyond a certain number of digits. If that is not acceptable for
your application, you probably shouldn't be using float types at all.

In your specific example, due to the lack of precision, float64 cannot
distinguish between 123.424999999999993 and 123.425000000000001:
http://play.golang.org/p/PAf89JrpYt

Shameless plug: you could try using the library I wrote for multi-precision
decimal arithmetic (project page: http://code.google.com/p/godec/ ) .
Actually it doesn't (yet) have a method for "just rounding", but dividing
by 1 with scale 2 and half up rounding should give you the desired result.
(I might add a separate method for just rounding later, although I think in
most cases where rounding is needed it is in combination with a division
anyway.)

Here is an example:

package main

import "fmt"

func main() {
d1, ok1 := new(dec.Dec).SetString("123.424999999999993")
d2, ok2 := new(dec.Dec).SetString("123.425000000000001")
if !ok1 || !ok2 {
panic("Invalid number")
}
one := dec.NewDecInt64(1)
r1 := new(dec.Dec).Quo(d1, one, dec.ScaleFixed2, dec.RoundHalfUp)
r2 := new(dec.Dec).Quo(d2, one, dec.ScaleFixed2, dec.RoundHalfUp)
fmt.Println(r1, r2)
}

Peter

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 int64
func Round(value float64) int64 {
if value < 0.0 {
value -= 0.5
} else {
value += 0.5
}
return int64(value)
}
--

--
•  at Oct 17, 2012 at 9:39 am ⇧

Am Sonntag, 1. Januar 2012 00:02:34 UTC+1 schrieb Archos:
Is there any similar function to "round" in JavaScript?
To return the value of a number rounded to the nearest integer.

My implementation:
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))
}

--
•  at Oct 17, 2012 at 3:53 pm ⇧
Johann - but as i wrote before, this code have bug:
http://play.golang.org/p/sA7h6XKDFt
Float types have limited precisions,
So I think that function round should behave like fmt.Sprinft( "%.2f",
floatNumber ) even if its not correct for smallest numbers.

ps. I'm put Round and RoundPrec in my small lib for convert and format
floats: https://github.com/DeyV/gotools

-DeyV

W dniu środa, 17 października 2012 11:39:38 UTC+2 użytkownik Johann Höchtl
napisał:

Am Sonntag, 1. Januar 2012 00:02:34 UTC+1 schrieb Archos:
Is there any similar function to "round" in JavaScript?
To return the value of a number rounded to the nearest integer.

My implementation:
--
•  at Oct 18, 2012 at 9:14 am ⇧
@Dawid: Is there a reason you didn't use http://play.golang.org/p/ZmFfr07oHp in

Does your version you chose handle negative numbers correctly now?
On Wednesday, 17 October 2012 17:24:45 UTC+2, Dawid Polak wrote:

Johann - but as i wrote before, this code have bug:
http://play.golang.org/p/sA7h6XKDFt
Float types have limited precisions,
So I think that function round should behave like fmt.Sprinft( "%.2f",
floatNumber ) even if its not correct for smallest numbers.

ps. I'm put Round and RoundPrec in my small lib for convert and format
floats: https://github.com/DeyV/gotools

-DeyV

W dniu środa, 17 października 2012 11:39:38 UTC+2 użytkownik Johann Höchtl
napisał:

Am Sonntag, 1. Januar 2012 00:02:34 UTC+1 schrieb Archos:
Is there any similar function to "round" in JavaScript?
To return the value of a number rounded to the nearest integer.

My implementation:
--
•  at Oct 18, 2012 at 1:22 pm ⇧
You are right - this is faster and simpler version - I'm used it now.

-DeyV

W dniu czwartek, 18 października 2012 11:14:18 UTC+2 użytkownik Rory
McGuire napisał:
@Dawid: Is there a reason you didn't use

Does your version you chose handle negative numbers correctly now?
On Wednesday, 17 October 2012 17:24:45 UTC+2, Dawid Polak wrote:

Johann - but as i wrote before, this code have bug:
http://play.golang.org/p/sA7h6XKDFt
Float types have limited precisions,
So I think that function round should behave like fmt.Sprinft( "%.2f",
floatNumber ) even if its not correct for smallest numbers.

ps. I'm put Round and RoundPrec in my small lib for convert and format
floats: https://github.com/DeyV/gotools

-DeyV

W dniu środa, 17 października 2012 11:39:38 UTC+2 użytkownik Johann
Höchtl napisał:

Am Sonntag, 1. Januar 2012 00:02:34 UTC+1 schrieb Archos:
Is there any similar function to "round" in JavaScript?
To return the value of a number rounded to the nearest integer.

My implementation:
--
•  at Oct 18, 2012 at 9:19 pm ⇧
But also less correct.

http://play.golang.org/p/PUGBfFBV2i

On 18/10/2012, at 11:52 PM, "Dawid Polak" wrote:

You are right - this is faster and simpler version - I'm used it now.

-DeyV

W dniu czwartek, 18 października 2012 11:14:18 UTC+2 użytkownik Rory McGuire napisał:
@Dawid: Is there a reason you didn't use http://play.golang.org/p/ZmFfr07oHp in your library?

Does your version you chose handle negative numbers correctly now?

On Wednesday, 17 October 2012 17:24:45 UTC+2, Dawid Polak wrote:
Johann - but as i wrote before, this code have bug:
http://play.golang.org/p/sA7h6XKDFt
Float types have limited precisions,
So I think that function round should behave like fmt.Sprinft( "%.2f", floatNumber ) even if its not correct for smallest numbers.

ps. I'm put Round and RoundPrec in my small lib for convert and format floats: https://github.com/DeyV/gotools

-DeyV

W dniu środa, 17 października 2012 11:39:38 UTC+2 użytkownik Johann Höchtl napisał:

Am Sonntag, 1. Januar 2012 00:02:34 UTC+1 schrieb Archos:
Is there any similar function to "round" in JavaScript?
To return the value of a number rounded to the nearest integer.

My implementation:

--

--
•  at Oct 19, 2012 at 8:49 am ⇧

On Thursday, 18 October 2012 23:19:30 UTC+2, kortschak wrote:

But also less correct.
Yes for values bigger than int32's hightest positive value but the float
one in that example is wrong for all negative numbers, here is the fixed
one:
http://play.golang.org/p/pn2ZNK30XS

func RoundViaFloat(x float64, prec int) float64 {
var rounder float64
pow := math.Pow(10, float64(prec))
intermed := x * pow
_, frac := math.Modf(intermed)
x = .5
if frac < 0.0 {
x=-.5
}
if frac >= x {
rounder = math.Ceil(intermed)
} else {
rounder = math.Floor(intermed)
}

return rounder / pow
}

--
•  at Oct 19, 2012 at 8:56 am ⇧
@Dawid, the RoundViaFloat function is now the most accurate.

On Friday, 19 October 2012 10:49:17 UTC+2, Rory McGuire wrote:
On Thursday, 18 October 2012 23:19:30 UTC+2, kortschak wrote:

But also less correct.
Yes for values bigger than int32's hightest positive value but the float
one in that example is wrong for all negative numbers, here is the fixed
one:
http://play.golang.org/p/pn2ZNK30XS

func RoundViaFloat(x float64, prec int) float64 {
var rounder float64
pow := math.Pow(10, float64(prec))
intermed := x * pow
_, frac := math.Modf(intermed)
x = .5
if frac < 0.0 {
x=-.5
}
if frac >= x {
rounder = math.Ceil(intermed)
} else {
rounder = math.Floor(intermed)
}

return rounder / pow
}
--
•  at Oct 19, 2012 at 3:23 pm ⇧
Unfortunately - even in this version is bug:
Check this: http://play.golang.org/p/S-YiMAcZ2t

I hope that now RoundPrec is correct

-DeyV

W dniu piątek, 19 października 2012 10:50:33 UTC+2 użytkownik Rory McGuire
napisał:
@Dawid, the RoundViaFloat function is now the most accurate.

On Friday, 19 October 2012 10:49:17 UTC+2, Rory McGuire wrote:
On Thursday, 18 October 2012 23:19:30 UTC+2, kortschak wrote:

But also less correct.
Yes for values bigger than int32's hightest positive value but the float
one in that example is wrong for all negative numbers, here is the fixed
one:
http://play.golang.org/p/pn2ZNK30XS
--
•  at Oct 19, 2012 at 4:17 pm ⇧
Hope so too.
http://play.golang.org/p/S654PxAe_N fixes the problem for the other
function as well.
On 19 Oct 2012 5:18 PM, "Dawid Polak" wrote:

Unfortunately - even in this version is bug:
Check this: http://play.golang.org/p/S-YiMAcZ2t

I hope that now RoundPrec is correct

-DeyV

W dniu piątek, 19 października 2012 10:50:33 UTC+2 użytkownik Rory McGuire
napisał:
@Dawid, the RoundViaFloat function is now the most accurate.

On Friday, 19 October 2012 10:49:17 UTC+2, Rory McGuire wrote:
On Thursday, 18 October 2012 23:19:30 UTC+2, kortschak wrote:

But also less correct.
Yes for values bigger than int32's hightest positive value but the float
one in that example is wrong for all negative numbers, here is the fixed
one:
http://play.golang.org/p/**pn2ZNK30XS<http://play.golang.org/p/pn2ZNK30XS>
--
--
•  at Oct 21, 2012 at 1:58 am ⇧
http://play.golang.org/p/cwJj8ZJUhl
On Friday, October 19, 2012 12:12:20 PM UTC-4, Rory McGuire wrote:

Hope so too.
http://play.golang.org/p/S654PxAe_N fixes the problem for the other
function as well.
On 19 Oct 2012 5:18 PM, "Dawid Polak" <da...@mentax.pl <javascript:>>
wrote:
Unfortunately - even in this version is bug:
Check this: http://play.golang.org/p/S-YiMAcZ2t

I hope that now RoundPrec is correct

-DeyV

W dniu piątek, 19 października 2012 10:50:33 UTC+2 użytkownik Rory
McGuire napisał:
@Dawid, the RoundViaFloat function is now the most accurate.

On Friday, 19 October 2012 10:49:17 UTC+2, Rory McGuire wrote:
On Thursday, 18 October 2012 23:19:30 UTC+2, kortschak wrote:

But also less correct.
Yes for values bigger than int32's hightest positive value but the
float one in that example is wrong for all negative numbers, here is the
fixed one:
http://play.golang.org/p/**pn2ZNK30XS<http://play.golang.org/p/pn2ZNK30XS>
--
--
•  at Jul 15, 2013 at 9:18 pm ⇧
+1 Your Version Work fine to me, Joubin

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
•  at Jul 16, 2013 at 3:59 am ⇧
I believe a lot of rounding functions also choose a random rounding
direction (or base it off the previous number) if the decimal part is 0.5
exactly
On Monday, July 15, 2013 1:10:51 PM UTC-7, Christian Espinoza wrote:

+1 Your Version Work fine to me, Joubin
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
•  at Jul 16, 2013 at 4:04 am ⇧

On Mon, Jul 15, 2013 at 8:59 PM, Brendan Tracey wrote:
I believe a lot of rounding functions also choose a random rounding
direction (or base it off the previous number) if the decimal part is 0.5
exactly
The most common method for rounding 0.5 is round-half-to-even (i.e.,
least significant bit goes to zero). That is what IEEE 754 specifies,
and that is the floating point format used on all current processors.

Ian

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
•  at Jul 16, 2013 at 4:07 am ⇧
I stand corrected.
On Jul 15, 2013, at 9:04 PM, Ian Lance Taylor wrote:

On Mon, Jul 15, 2013 at 8:59 PM, Brendan Tracey
wrote:
I believe a lot of rounding functions also choose a random rounding
direction (or base it off the previous number) if the decimal part is 0.5
exactly
The most common method for rounding 0.5 is round-half-to-even (i.e.,
least significant bit goes to zero). That is what IEEE 754 specifies,
and that is the floating point format used on all current processors.

Ian
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
•  at Jul 16, 2013 at 10:40 am ⇧

On 16 July 2013 05:04, Ian Lance Taylor wrote:
On Mon, Jul 15, 2013 at 8:59 PM, Brendan Tracey
wrote:
I believe a lot of rounding functions also choose a random rounding
direction (or base it off the previous number) if the decimal part is 0.5
exactly
The most common method for rounding 0.5 is round-half-to-even (i.e.,
least significant bit goes to zero). That is what IEEE 754 specifies,
and that is the floating point format used on all current processors.
It would be nice if that rounding functionality was available
in the standard library (perhaps it is though and I've missed it?)

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.

## Related Discussions

Discussion Overview
 group golang-nuts categories go posted Oct 16, '12 at 5:53p active Jul 16, '13 at 10:40a posts 22 users 12 website golang.org

### 12 users in discussion

Content

People

Support

Translate

site design / logo © 2022 Grokbase