Replicate v1.x locale formatting in v2
mainv2 has dropped built-in support for locales to remain dependency-free. To replicate the formatting behavior from v1.x, you must create a custom formatter that wraps the native Intl API using toDecimal.
import { toDecimal } from 'dinero.js';
function createIntlFormatter(locale, options = {}) {
function transformer({ value, currency }) {
return Number(value).toLocaleString(locale, {
...options,
style: 'currency',
currency: currency.code,
});
}
return function formatter(dineroObject) {
return toDecimal(dineroObject, transformer);
};
}
export const intlFormat = createIntlFormatter('en-US');
// Usage:
// intlFormat(dineroObject) -> "$5.00"import { toDecimal } from 'dinero.js';
function createIntlFormatter(locale, options = {}) {
function transformer({ value, currency }) {
return Number(value).toLocaleString(locale, {
...options,
style: 'currency',
currency: currency.code,
});
}
return function formatter(dineroObject) {
return toDecimal(dineroObject, transformer);
};
}
export const intlFormat = createIntlFormatter('en-US');