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

[Bug]: toDecimal() returns .0 for exponent 0 currencies and should return an integer #751

Open
2 tasks done
jasongitmail opened this issue Aug 5, 2023 · 5 comments
Open
2 tasks done
Assignees
Labels
bug Something isn't working

Comments

@jasongitmail
Copy link

jasongitmail commented Aug 5, 2023

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

  1. Create a dinero object for a base 10, exponent 0 currency (e.g. JPY or VND)
  2. 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
@jasongitmail jasongitmail added the bug Something isn't working label Aug 5, 2023
@jasongitmail jasongitmail changed the title [Bug]: toDecimal() returns .0 for exponent 0 currencies and should have no decimal [Bug]: toDecimal() returns .0 for exponent 0 currencies and should return an integer Aug 5, 2023
@ng-hai
Copy link

ng-hai commented Aug 28, 2023

I also get the same behavior
image

@ElChapitan
Copy link

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.

@ng-hai
Copy link

ng-hai commented Sep 28, 2023

Agree with @ElChapitan, I had removed the scale option and had let the exponent do the thing. JPY looks normal now

image

@ElChapitan
Copy link

Agree with @ElChapitan, I had removed the scale option and had let the exponent 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.

@ng-hai
Copy link

ng-hai commented Sep 28, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants