Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Arggh Golang does not include a round function in the standard math package. So I wrote a quick one.
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)
if div >= roundOn {
round = math.Ceil(digit)
} else {
round = math.Floor(digit)
}
newVal = round / pow
return
}
func main() {
log.Println(Round(123.555555, .5, 3))
log.Println(Round(123.558, .5, 2))
}
@xh3b4sd

This comment has been minimized.

Show comment
Hide comment
@xh3b4sd

xh3b4sd Aug 7, 2014

And here is the link for the playground http://play.golang.org/p/yjfShH_uEy

xh3b4sd commented Aug 7, 2014

And here is the link for the playground http://play.golang.org/p/yjfShH_uEy

@ianchildress

This comment has been minimized.

Show comment
Hide comment
@ianchildress

ianchildress Oct 4, 2014

YOU ARE AWESOME. Thank you thank you thank you thank you. Thank you.

YOU ARE AWESOME. Thank you thank you thank you thank you. Thank you.

@pelegm

This comment has been minimized.

Show comment
Hide comment
@pelegm

pelegm Oct 23, 2014

I think this does not work properly for negative values.

pelegm commented Oct 23, 2014

I think this does not work properly for negative values.

@pelegm

This comment has been minimized.

Show comment
Hide comment
@pelegm

pelegm Oct 23, 2014

I think that my fork solves that issue.

pelegm commented Oct 23, 2014

I think that my fork solves that issue.

@korya

This comment has been minimized.

Show comment
Hide comment
@korya

korya Feb 11, 2015

The solution is way overcomplicated.

IMHO for simple round function it's better to use the straightforward solution:

func Round(f float64) float64 {
    return math.Floor(f + .5)
}

Want to round to a specific precision?

func RoundPlus(f float64, places int) (float64) {
    shift := math.Pow(10, float64(places))
    return Round(f * shift) / shift;    
}

Want to specify the roundOn value?
I'll leave it as an exercise for you.

Here is a goplay

korya commented Feb 11, 2015

The solution is way overcomplicated.

IMHO for simple round function it's better to use the straightforward solution:

func Round(f float64) float64 {
    return math.Floor(f + .5)
}

Want to round to a specific precision?

func RoundPlus(f float64, places int) (float64) {
    shift := math.Pow(10, float64(places))
    return Round(f * shift) / shift;    
}

Want to specify the roundOn value?
I'll leave it as an exercise for you.

Here is a goplay

@brydavis

This comment has been minimized.

Show comment
Hide comment
@brydavis

brydavis May 24, 2015

I agree with solution by @korya. Thanks to everyone, though.

I agree with solution by @korya. Thanks to everyone, though.

@AlasdairF

This comment has been minimized.

Show comment
Hide comment
@AlasdairF

AlasdairF May 26, 2015

Without the math package:

func round(v float64, decimals int) float64 {
     var pow float64 = 1
     for i:=0; i<decimals; i++ {
         pow *= 10
     }
     return float64(int((v * pow) + 0.5)) / pow
}

Without the math package:

func round(v float64, decimals int) float64 {
     var pow float64 = 1
     for i:=0; i<decimals; i++ {
         pow *= 10
     }
     return float64(int((v * pow) + 0.5)) / pow
}
@tgpfeiffer

This comment has been minimized.

Show comment
Hide comment
@tgpfeiffer

tgpfeiffer Jul 17, 2015

I think the solution from @korya doesn't work for negative numbers. This one should do:

func(a float64) float64 {
    if a < 0 {
        return math.Ceil(a - 0.5)
    }
    return math.Floor(a + 0.5)
}

I think the solution from @korya doesn't work for negative numbers. This one should do:

func(a float64) float64 {
    if a < 0 {
        return math.Ceil(a - 0.5)
    }
    return math.Floor(a + 0.5)
}
@siddontang

This comment has been minimized.

Show comment
Hide comment
@siddontang

siddontang Sep 3, 2015

Thanks @DavidVaini
but the result is not the same as prevailing GUN c rint, e.g,

rint(1.5) -> 2
rint(2.5) -> 2

but we will get 2 and 3 with yours with roundOn = 0.5 and places = 0.

you can see the reference here http://www.gnu.org/software/libc/manual/html_node/Rounding.html

I just write a simple one here: https://gist.github.com/siddontang/1806573b9a8574989ccb

Thanks @DavidVaini
but the result is not the same as prevailing GUN c rint, e.g,

rint(1.5) -> 2
rint(2.5) -> 2

but we will get 2 and 3 with yours with roundOn = 0.5 and places = 0.

you can see the reference here http://www.gnu.org/software/libc/manual/html_node/Rounding.html

I just write a simple one here: https://gist.github.com/siddontang/1806573b9a8574989ccb

@dim13

This comment has been minimized.

Show comment
Hide comment
@dim13

dim13 Feb 8, 2017

Note: this implementation fails for Round(-3.333, 0.5, 2) -- expected -3.33, got -3.34

Simple \sgn(y) \left\lfloor \left| y \right| + 0.5 \right\rfloor would be enough.

dim13 commented Feb 8, 2017

Note: this implementation fails for Round(-3.333, 0.5, 2) -- expected -3.33, got -3.34

Simple \sgn(y) \left\lfloor \left| y \right| + 0.5 \right\rfloor would be enough.

@a-h

This comment has been minimized.

Show comment
Hide comment
@a-h

a-h Mar 17, 2017

After reading through many different and untested solutions, I decided to put together a package with implementations of ToEven (used in .Net, Python 3) rounding and AwayFromZero (used in Python 2) rounding.

It includes unit tests that demonstrate the expected behaviour of the implementation, and a comparison test with Python and a rough benchmark.

See https://github.com/a-h/round

Happy to take a pull request for ToPositiveInfinity (used by Java) and ToNegativeInfinity rounding if anyone cares about them.

a-h commented Mar 17, 2017

After reading through many different and untested solutions, I decided to put together a package with implementations of ToEven (used in .Net, Python 3) rounding and AwayFromZero (used in Python 2) rounding.

It includes unit tests that demonstrate the expected behaviour of the implementation, and a comparison test with Python and a rough benchmark.

See https://github.com/a-h/round

Happy to take a pull request for ToPositiveInfinity (used by Java) and ToNegativeInfinity rounding if anyone cares about them.

@DavidVaini

This comment has been minimized.

Show comment
Hide comment
@DavidVaini

DavidVaini Jul 28, 2017

hi guys, I never saw these comments until just now. The implementation was old and was something quick and dirty. Thus the code is in a gist snippet and not a repo. This worked for my case which was positive integers, but note it will fail with negative numbers. It looks like people have other implementations though they don't account for what decimal place you want to round on or what value you want to round on (.5 vs .9 or other use cases.

Owner

DavidVaini commented Jul 28, 2017

hi guys, I never saw these comments until just now. The implementation was old and was something quick and dirty. Thus the code is in a gist snippet and not a repo. This worked for my case which was positive integers, but note it will fail with negative numbers. It looks like people have other implementations though they don't account for what decimal place you want to round on or what value you want to round on (.5 vs .9 or other use cases.

@fedir

This comment has been minimized.

Show comment
Hide comment

fedir commented Feb 26, 2018

@korya +1

@CamilleFrazaoCDP

This comment has been minimized.

Show comment
Hide comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment