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

[Bug]: toUnits() swallows leading zeros on minor units

Open ameeuw opened this issue 1 year ago • 0 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Current behavior

The example given in the docs works as expected.

import { USD } from '@dinero.js/currencies';

const d1 = dinero({ amount: 1050, currency: USD });
const d2 = dinero({ amount: 10545, currency: USD, scale: 3 });

toUnits(d1); // [10, 50]
toUnits(d2); // [10, 545]

However if we change it to a leading zero in the minors, we lose this:

import { USD } from '@dinero.js/currencies';

const d1 = dinero({ amount: 1005, currency: USD });
const d2 = dinero({ amount: 10054, currency: USD, scale: 3 });

toUnits(d1); // [10, 5]
toUnits(d2); // [10, 54]

Expected behavior

import { USD } from '@dinero.js/currencies';

const d1 = dinero({ amount: 1005, currency: USD });
const d2 = dinero({ amount: 10054, currency: USD, scale: 3 });

toUnits(d1); // [10, 05]
toUnits(d2); // [10, 054]

As this is not possible, the "toUnits()" should return a string in order to preserve leading zeros.

Steps to reproduce

Use the above "Current behavior" example.

Version

2.0.0-alpha.14

Environment

Nodejs 14

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

ameeuw avatar Sep 05 '23 13:09 ameeuw