[Bug]: toDecimal() returns `.0` for exponent 0 currencies and should return an integer
jasongitmail opened this issue · comments
Is there an existing issue for this?
- I have searched the existing issues
Current behavior
// Currencies object
"JPY": {
"code": "JPY",
"base": 10,
"exponent": 0
},
const dineroObj = {
"amount": 300,
"currency": {
"code": "JPY",
"base": 10,
"exponent": 0
},
"scale": 0
}
console.log(toDecimal(dineroObj))
// result is 300.0, expected 300
This same behavior occurs for other exponent 0 currencies.
Expected behavior
I would expect an integer value to be returned for "exponent 0" currencies, such as 300
for the above example.
Steps to reproduce
- Create a dinero object for a base 10, exponent 0 currency (e.g. JPY or VND)
- Pass it to
toDecimal(dineroObj))
and view the result.
Version
2.0.0-alpha
Note: I'm using Dinero's default JS numbers, not BigInt or other.
Environment
Node v20
Code of Conduct
- I agree to follow this project's Code of Conduct
It looked like the fractional part of the string is getting appended even if the scale is zero. Upon further reflection, might want to limit based on the currency exponent rather than the scale, but I'll hope for some input from the maintainers on that.
Agree with @ElChapitan, I had removed the scale
option and had let the exponent
do the thing. JPY looks normal now
Agree with @ElChapitan, I had removed the
scale
option and had let theexponent
do the thing. JPY looks normal now
The part that I'm not familiar with, but am curious. Is there a point where you'd want to pay attention to exponent instead? For instance, you could create a dinero object for the dollar that is scale 0 (but the currency is exponent 2). In that case, should toDecimal
return 1
or 1.00
.
At first I used @dinero/currency
to check for specific currency to apply format. Our backend also returned a decimals
config, I thought it would be the scale
value, but it was not. The backend data follow ISO 4217 standard so it would be the same config for Currency object, no need that scale
anymore.