Skip to content

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

@ameeuw

Description

@ameeuw

Is there an existing issue for this?

  • 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

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

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions