Skip to content

Instantly share code, notes, and snippets.

@joaohcrangel
Created June 3, 2022 20:21
Show Gist options
  • Save joaohcrangel/47810bc4ff2d78ce9f9cf908a43d6e9b to your computer and use it in GitHub Desktop.
Save joaohcrangel/47810bc4ff2d78ce9f9cf908a43d6e9b to your computer and use it in GitHub Desktop.
Format number to currency in TypeScript
export function formatCurrency(
initialValue: number,
options?: {
currency?: string;
precision?: number;
locale?: string;
}
): string {
const currencies = {
USD: '$',
EUR: '€',
BRL: 'R$',
};
const { currency, precision, locale } = Object.assign(
{
currency: 'BRL',
precision: 2,
locale: 'pt-BR',
},
options
);
const str = String(initialValue);
const splited = str.split('.');
const cents =
splited.length > 1
? String(splited[1]).padEnd(precision, '0')
: '0'.repeat(precision);
const value = splited[0];
var chunks: string[] = [];
for (let i = value.length; i > 0; i -= 3) {
chunks.push(value.substring(i, i - 3));
}
chunks.reverse();
switch (locale) {
case 'pt-BR':
return `${currencies[currency]} ${chunks.join('.')}${
precision > 0 ? ',' + cents : ''
}`;
default:
return `${currencies[currency]} ${chunks.join(',')}${
precision > 0 ? '.' + cents : ''
}`;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment