currency.js icon indicating copy to clipboard operation
currency.js copied to clipboard

Precision with fromCents results in incorrect values

Open hexagonal-sun opened this issue 3 years ago • 3 comments

Hi @scurker

I'm using fromCents with a precison option and I'm getting unexpected results:

> currency(123, {fromCents: true, precision: 4}).format()
'$0.0123'

I would have expected $1.2300.

Thanks, Matt

hexagonal-sun avatar May 18 '21 18:05 hexagonal-sun

Additionally, I would have also expected to be able to add decimal amounts to cents:

currency(123.98, {fromCents: true, precision: 4}).format()

Which would yield: $1.2398, I'm currently getting $0.0124. Thanks!

hexagonal-sun avatar May 18 '21 18:05 hexagonal-sun

This is expected behavior.

currency(1, { fromCents: true }) // => $0.01
currency(123, { fromCents: true }) // => $1.23
currency(123, { fromCents: true, precision: 3 }) // => $0.123

scurker avatar May 19 '21 00:05 scurker

In that case, I'd suggest that the name fronCents is misleading. I'd have thought that even with a higher precision, 123 cents would still result in $1.23.

hexagonal-sun avatar May 27 '21 07:05 hexagonal-sun