Click here for v1.x documentation.
Dinero.js
Dinero.js version

toUnit

number

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

NameTypeDescriptionRequired
dineroObjectDinero<TAmount>

The Dinero object to format.

Yes
optionsRoundingOptions<TAmount>

A mapping of options.

No
options.digitsTAmount

The number of fraction digits to round to.

No
options.roundRoundingMode

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