Get the amount of a Dinero object in units.
This function returns the total amount divided into each unit and sub-unit, as an array. For example, an object representing $10.45 expressed as 1045
(with currency USD
and no custom scale
) would return [10, 45]
for 10 dollars and 45 cents.
When specifying multiple bases, the function returns as many units as necessary.
Copy linkParameters
Name | Type | Description | Required |
---|---|---|---|
dineroObject | Dinero<TAmount> | The Dinero object to format. | Yes |
Copy linkCode examples
Copy linkFormat an object in units
import { dinero, toUnits } from 'dinero.js';
import { USD } from '@dinero.js/currencies';
const d = dinero({ amount: 1050, currency: USD });
toUnits(d); // [10, 50]
Copy linkFormat an object with a custom scale
import { dinero, toUnits } from 'dinero.js';
import { USD } from '@dinero.js/currencies';
const d = dinero({ amount: 10545, currency: USD, scale: 3 });
toUnits(d); // [10, 545]
Copy linkFormat a non-decimal object
import { dinero, toUnits } from 'dinero.js';
const GRD = { code: 'GRD', base: 6, exponent: 1 };
const d = dinero({ amount: 9, currency: GRD });
toUnits(d); // [1, 3]
Copy linkFormat an object with multiple subdivisions
import { dinero, toUnits } from 'dinero.js';
const GBP = { code: 'GBP', base: [20, 12], exponent: 1 };
const d = dinero({ amount: 267, currency: GBP });
toUnits(d); // [1, 2, 3]