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
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 numberWithCommas(x) {
2 return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
3}
4
1var value = 2.9802453587962963;
2var wholeNum = Math.floor(value);
3console.log(wholeNum); // output ==> 2
1var x = parseFloat('9.656');
2
3x.toFixed(0); // returns 10
4x.toFixed(2); // returns 9.66
5x.toFixed(4); // returns 9.6560
6x.toFixed(6);