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
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 */
1function floatToEuro(float){
2 var euroCurrency
3 euroCurrency = '\u20AC' + float.toLocaleString('nl-NL',{minimumFractionDigits: 2});
4 console.log(euroCurrency);
5 return euroCurrency;
6};
1/* npm install format-money-js */
2
3const { FormatMoney } = require('format-money-js');
4
5const fm = new FormatMoney({
6 decimals: 2
7});
8
9console.log(fm.from(
10 12345.67,
11 { symbol: '$' },
12 true // Parse, return object
13 )
14);
15/* return object:
16{
17 source: 12345.67,
18 negative: false,
19 fullAmount: '12,345.67',
20 amount: '12,345',
21 decimals: '.67',
22 symbol: '$'
23}*/