Get the amount of a Dinero object in major currency unit.
By default, the number of represented fraction digits depends on the amount and scale of the Dinero object. You can specify how many fraction digits you want to represent and pass a rounding function.
For convenience, Dinero.js provides the following rounding functions: up
, down
, halfUp
, halfDown
, halfOdd
, halfEven
(bankers rounding), halfTowardsZero
, and halfAwayFromZero
.
Copy linkParameters
Name | Type | Description | Required |
---|---|---|---|
dineroObject | Dinero<TAmount> | The Dinero object to format. | Yes |
options | RoundingOptions<TAmount> | A mapping of options. | No |
options.digits | TAmount | The number of fraction digits to round to. | No |
options.round | RoundingMode | The rounding function to use. | No |
Copy linkCode examples
Copy linkFormat an object in major currency unit
import { dinero, toUnit } from 'dinero.js';
import { USD } from '@dinero.js/currencies';
const d = dinero({ amount: 1050, currency: USD });
toUnit(d); // 10.5
Copy linkFormat an object with a custom scale
import { dinero, toUnit } from 'dinero.js';
import { USD } from '@dinero.js/currencies';
const d = dinero({ amount: 10545, currency: USD, scale: 3 });
toUnit(d); // 10.545
Copy linkFormat an object rounded to one fraction digit
import { dinero, toUnit, down } from 'dinero.js';
import { USD } from '@dinero.js/currencies';
const d = dinero({ amount: 1055, currency: USD });
toUnit(d, { digits: 1, round: down }); // 10.5