Comments (6)
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.
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.
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.
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.
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.
No float, it has been explained before.
from money.
Related Issues (20)
- `roundToUnit` does not handles amounts less than 1 unit HOT 3
- SwapExchange does not work with small floating point numbers HOT 1
- PHPUnit comparator breaks under PHPUnit 10/comparator 5 HOT 1
- Aggregate Currencies unexpected behaviour HOT 1
- Bring back HRK currency HOT 3
- numberFormat refuses to use comma as decimal separator. HOT 2
- Possibility to allocate with custom rounding parameter
- Leading zeros. Confusing amount after parsing and formatting HOT 1
- Cryptocurrency Advice HOT 2
- Documentation is out of date HOT 1
- Question about Yemenis YER cent amount HOT 1
- Incorrect βminorUnitβ Value for Iranian Rial (IRR) HOT 1
- "psr/cache" 3 can not be installed because of cache/tag-interop HOT 2
- Documentation needs details on tax calculation HOT 3
- ISK convert gives wrong amount (x100) HOT 1
- This field is attempting to submit the form on page load for some reason HOT 1
- . HOT 1
- Official website is not working HOT 1
- Enhancement:: Allow multiply by float numbers less than 1 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from money.