1const formatToCurrency = amount => {
2 return "$" + amount.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, "$&,");
3};
4formatToCurrency(12.34546); //"$12.35"
5formatToCurrency(42345255.356); //"$42,345,255.36
1function formatToCurrency(amount){
2 return (amount).toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
3}
4formatToCurrency(12.34546); //"12.35"
5formatToCurrency(42345255.356); //"42,345,255.36"
1const formatter = new Intl.NumberFormat('en-US', {
2 style: 'currency',
3 currency: 'USD',
4 minimumFractionDigits: 2
5})
6
7formatter.format(1000) // "$1,000.00"
8formatter.format(10) // "$10.00"
9formatter.format(123233000) // "$123,233,000.00"
1const formatter = new Intl.NumberFormat('en-ID', {
2 style: 'currency',
3 currency: 'IDR'
4}).format(10000000)
5.replace(/[IDR]/gi, '')
6.replace(/(\.+\d{2})/, '')
7.trimLeft()
8
9
10console.log(`Rp ${formatter}`)
1(1234567.8).toFixed(2).replace(/(\d)(?=(\d{2})+\d\.)/g, '$1,') // "12,34,567.80"