Skip to content

toDecimal

Get the amount of a Dinero object in a stringified decimal representation.

The number of decimal places depends on the scale of your object—or, when unspecified, the exponent of its currency.

INFO

You can only use this function with Dinero objects that are single-based and use a decimal currency.

Parameters

NameTypeDescriptionRequired
dineroObjectDinero<TAmount>The Dinero object to format.Yes
transformerTransformer<TAmount, TOutput>An optional transformer function.No

Code examples

Format an object in decimal format

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

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

toDecimal(d1); // "10.50"
toDecimal(d2); // "10.545"

Use a custom transformer

If you need to further transform the value before returning it, you can pass a custom function.

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

const d = dinero({ amount: 1050, currency: USD });

toDecimal(d, ({ value, currency }) => `${currency.code} ${value}`); // "USD 10.50"

Released under the MIT License.