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

Unexpected results with arithmatic when using fromCents

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

Hi @scurker ,

I've found some inconsistencies when applying arithmetic after using fromCents:

> c(451, {fromCents: true}).subtract(1).format()
'$4.50'

I would have expected the value to be $3.50; the same is also true for add. Perhaps this is expected behavior and creating a currency object using fromCents means that arithmetic performed is in cent amounts rather than dollar amounts. If that's the case, I'd say that the name fromCents is misleading as it implies you get a regular currency object, except that the initial value is converted from cents; there's nothing to indicate that arithmatic is done in cent values.

Thanks.

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

This was discussed on this issue: https://github.com/scurker/currency.js/issues/260#issuecomment-659675669

The currency object inherits all of the options passed into the initial function. I would think the opposite behavior would be more confusing, as I would think all of your nominal values would be similar - but I haven't actively used fromCents much myself.

scurker avatar May 18 '21 23:05 scurker