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

No trailing zero when there is no fraction? #2

Closed
devnomic opened this issue Feb 19, 2023 · 4 comments
Closed

No trailing zero when there is no fraction? #2

devnomic opened this issue Feb 19, 2023 · 4 comments

Comments

@devnomic
Copy link

const a = dn.from("5", 2);
dn.format(a, { digits: 2, trailingZeros: true })
// Expected 5.00
// Result 5

I was expecting number toFixed kinda behavior.
Shouldn't it also show trailing zero for consistency?
or is it intended?

const a = dn.from("5", 4);
const b = dn.from("5.1", 4);
const c = dn.from("5.12", 4);
const d = dn.from("5.123", 4);

console.log(dn.format(a, { digits: 4, trailingZeros: true })); // 5
console.log(dn.format(b, { digits: 4, trailingZeros: true })); // 5.1000
console.log(dn.format(c, { digits: 4, trailingZeros: true })); // 5.1200
console.log(dn.format(d, { digits: 4, trailingZeros: true })); // 5.1230
@bpierre
Copy link
Owner

bpierre commented Feb 20, 2023

You’re absolutely right! It is fixed in 2.4.1 https://github.com/bpierre/dnum/releases/tag/v2.4.1

Thanks for reporting it 🙏

@devnomic
Copy link
Author

Seems there is bug, when decimals is zero it doesn't work

const a = [0n, 0]
dn.format(a, { digits: 2, trailingZeros: true }) 
// Result: 0
// Expected: 0.00

const b = dn.from(5); // [5n,0]
dn.format(b, { digits: 2, trailingZeros: true }) 
// Result: 5
// Expected: 5.00

It only works if decimals > 0

const c = [0n, 1]
console.log(dn.format(c, { digits: 2, trailingZeros: true }));
// Result: 0.00
// Expected: 0.00

@bpierre
Copy link
Owner

bpierre commented Feb 21, 2023

Ah yes, it’s a bit of a weird case because if the decimals are lower than digits and trailingZeros is true, then the difference in digits will always show as a series of 0. But I agree we should respect digits and trailingZeros, it makes more sense. Doing the change!

@bpierre
Copy link
Owner

bpierre commented Feb 21, 2023

Pulished as 2.5.0, thank you 🙏 https://github.com/bpierre/dnum/releases/tag/v2.5.0

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