The server provides JSON containing a string such as 1234567.89
.
How do I convert it to $ 1,234,568
?
Based on How can I format numbers as money in JavaScript?, I can convert it to $ 1,234,567.89
.
return "$ " + parseFloat(n).toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,");
While not perfect because it rounds down versus rounds to nearest dollar, I would have thought the following would work, but it does not:
return "$ " + parseFloat(n).toFixed(0).replace(/(\d)(?=(\d{3})+\.)/g, "$1,");
I also unsuccessfully tried the following:
return "$ " + Math.round(parseFloat(n)).replace(/(\d)(?=(\d{3})+\.)/g, "$1,");