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"
9
10
11-----------------------------------------------------
12const formatter = new Intl.NumberFormat('vi-VN', {
13 style: 'currency',
14 currency: 'VND',
15 minimumFractionDigits: 0
16})
17
18
19$('.total-price').html(formatter.format(price)); // 25.000đ
20
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"
1// Create our number formatter.
2var formatter = new Intl.NumberFormat('en-US', {
3 style: 'currency',
4 currency: 'USD',
5});
6
7formatter.format(2500); /* $2,500.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}`)