Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with dividedBy and MultipliedBy? #59

Closed
satrio1256 opened this issue Aug 1, 2021 · 3 comments
Closed

Issue with dividedBy and MultipliedBy? #59

satrio1256 opened this issue Aug 1, 2021 · 3 comments

Comments

@satrio1256
Copy link

Hi!
I'm currently trying to convert this following formula to my code

% increase = (total - (total - current)) ÷ total × 100

However it somehow give different result when I wrote dividedBy then multipliedBy and multipliedBy then dividedBy

$tc & $cc is BigDecimal Object
For testing : 
if $tc = 10 and $cc = 1, the result should be 10
if $tc = 10 and $cc = 2, the result should be 20

BigDecimal::of($tc->minus($tc->minus($cc)))->dividedBy($tc, 0, RoundingMode::HALF_UP)->multipliedBy('100') // always return 0 (incorrect result)
BigDecimal::of($tc->minus($tc->minus($cc)))->multipliedBy('100')->dividedBy($tc, 0, RoundingMode::HALF_UP) // correct result returned

I'm not quite sure if this behavior is intended or there is incorrect implementation from my end..

Thank you for creating this amazingly useful library though 😄

@satrio1256
Copy link
Author

Oh my bad, there is nothing wrong with your library.. The issue is my docker that did not refresh the code and makes it return 0

@BenMorel
Copy link
Member

BenMorel commented Aug 1, 2021

👍

Be careful with roundings, too, you’re rounding to 0 decimals here!

Maybe you could use BigRationals for calculations, then convert to a BigDecimal on the very last step.

@satrio1256
Copy link
Author

Got it, thanks for the advice 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants