Git Product home page Git Product logo

Comments (6)

wjzijderveld avatar wjzijderveld commented on August 16, 2024

The whole point of this library is to avoid using floats for money πŸ˜…
That's why it's using strings internally and has some helpers to allow the usage of integers (it's using strings to allow for numbers greater than PHP_INT_MAX).

> php -r 'echo var_dump(0.1+0.2);'
float(0.30000000000000004)

Also see https://www.php.net/manual/en/language.types.float.php#language.types.float and https://floating-point-gui.de

from money.

wilcol avatar wilcol commented on August 16, 2024

The whole point of this library is to avoid using floats for money πŸ˜… That's why it's using strings internally and has some helpers to allow the usage of integers (it's using strings to allow for numbers greater than PHP_INT_MAX).

> php -r 'echo var_dump(0.1+0.2);'
float(0.30000000000000004)

Also see https://www.php.net/manual/en/language.types.float.php#language.types.float and https://floating-point-gui.de

I'm also having the same issue as the OP. If the reason is to avoid floats, why is there a roundingMode parameter present in the multiply function? (int * int = int) But more importantly; why would multiplication with a float not be a valid usecase to cover off in this library? In my case it's being used for VAT calculations. As a quick/dirty fix I'm now casting floats to strings, but it would help to get guidance if the multiply (and divide) function(s) should be used with integers only. The manual (https://www.moneyphp.org/en/stable/features/operation.html#multiplication-division) is also not explicit on this. Happy to hear the thoughts on this from the team.

Thanks for supporting this great library!

from money.

wjzijderveld avatar wjzijderveld commented on August 16, 2024

The problem isn't decimals, it's floats, as they they aren't precise and can result in expected results.
$amount->multiply("0.3"); works just fine, casting to string isn't a dirty fix, it's the approach that allows the library to handle the calculations while keeping precision.

btw, disclaimer; I'm not part of this library, I've just been using it since Matthias first published it 12,5 years ago.

from money.

wilcol avatar wilcol commented on August 16, 2024

Thanks @wjzijderveld for your inputs and thoughts. I understand the unexpected results rounding and truncation can give, that's why I use this library myself as well :) I'm looking at this from a 'clean code' and naming perspective. In my view, it's inconsitent to only accept int | string. Either use string only (and document its possible values), or accept all posible types string | int | float | etc... This makes the method signature explicit without any need to guess... and also keeps the method backwards compatible since no deprecation notices were given... Anyway, interesting food for thought!

from money.

ivan88217 avatar ivan88217 commented on August 16, 2024

Thanks @wjzijderveld for your points.
Although I know avoid to use float is right, but I'm just confusing about different behavior from upgrade to new version.
It broke my legacy system when I upgrade laravel version from 6 to 9, and I can't find out the document for that.

I'm currently using it to calculate tax rates, so if it can't accept float, things will be a bit troublesome.

from money.

frederikbosch avatar frederikbosch commented on August 16, 2024

No float, it has been explained before.

from 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.