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

Round up returns wrong number #240

Closed
takihama opened this issue Dec 5, 2024 · 3 comments
Closed

Round up returns wrong number #240

takihama opened this issue Dec 5, 2024 · 3 comments

Comments

@takihama
Copy link

takihama commented Dec 5, 2024

I'm expecting the result of the roundUp to be 1.12, but it returns wrongly 1.13.

const { default: Decimal } = require('decimal.js');

const amountAvailable = 1.12;
const quantityArs = 1197.5384615384617;
const exchangeRate = 1069.2307692307693;

// Decimal
console.log(new Decimal(amountAvailable).times(exchangeRate).toNumber()); // 1197.5376
console.log(new Decimal(quantityArs).div(exchangeRate).toNumber()); // 1.12
console.log(new Decimal(quantityArs).div(exchangeRate).toDecimalPlaces(2, Decimal.ROUND_UP).toNumber()); // 1.13
console.log(new Decimal(1.12).toDecimalPlaces(2, Decimal.ROUND_UP).toNumber()); // 1.12

@MikeMcl
Copy link
Owner

MikeMcl commented Dec 5, 2024

Look first at the value of new Decimal(quantityArs).div(exchangeRate) and then consider again whether the rounding is incorrect.

@takihama
Copy link
Author

takihama commented Dec 6, 2024

Look first at the value of new Decimal(quantityArs).div(exchangeRate) and then consider again whether the rounding is incorrect.

That's exactly what I did, when I convert it to number with toNumber it returns 1.12, but when I round it up to two decimals ir wrongly returns 1.13

@MikeMcl
Copy link
Owner

MikeMcl commented Dec 6, 2024

No, the rounding is not incorrect.

Please post the exact value you are trying to round, i.e post the toString value of new Decimal(quantityArs).div(exchangeRate), then it should be obvious the rounding is correct.

@MikeMcl MikeMcl closed this as completed Jan 23, 2025
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