Coder Social home page Coder Social logo

Comments (5)

TimothyIp avatar TimothyIp commented on May 17, 2024 2

Hopefully, this convo will help for posterity if someone needs specific rounding for a use-case and how they can achieve this with NewFromFloat. Going to close this. Thanks!

from go-money.

Rhymond avatar Rhymond commented on May 17, 2024

Hi @TimothyIp
To be honest there was not much of a discussion about this new feature. The current formula:

int64(99.9999 * 10^(fraction))

The rounding is done by casting float to integer and this is how it works in Go.
Maybe @npinochet can give you a bit more details

from go-money.

npinochet avatar npinochet commented on May 17, 2024

The rounding is done by casting float to integer and this is how it works in Go.

That's correct, I made the function thinking more about simplicity rather than usability.
Maybe we can have the discussion here, I think rounding is aceptable in this case, the formula can be adjusted for it:

factor = 10^fraction
int64((99.9999 + 0.5 / factor) * factor)

from go-money.

TimothyIp avatar TimothyIp commented on May 17, 2024

Just want to give thanks to the both of you for taking the time out of your day for answering this question of mine.

I can see the winner being simplicity here. I just went down a rabbit hole of all the different ways you can round cents (round down, round up, banker's rounding, ...etc).

I could see it just being as simple as having the consumer provide the float into NewFromFloat already rounded to their specific use case. (Which will solve my use case)

As for changing the internals of the function, that part i'm unsure of. I'm with keeping the behaviour in line with Go, since this rounding topic can get quite opinionated.

An additional function could be added that would do the explicit rounding for a given float, something like NewFromFloatRounded.

from go-money.

npinochet avatar npinochet commented on May 17, 2024

Thank you for opening this discussion :)
I agree that there are many ways to handle rounding in regards to currencies, I believe it's better to keep it simple.

There may be cases where floats like 99.999999 may appear, but that could mean that somewhere you did some float arithmetics before converting them to Money, which beats the use of this library.

If rounding is really necesary, one could always pre round the float before passing it to the function (NewFromFloat(99.999999 + 0.005))

from go-money.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.