Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
-3
rated 0 times [  0] [ 3]  / answers: 1 / hits: 32122  / 9 Years ago, thu, september 24, 2015, 12:00:00
var num = 1629; // this represents $16.29
num.toLocaleString(en-US, {style:currency, currency:USD});
// outputs $1,629


So far this is as close as I can come. I tried all of the options that toLocaleString provides but there seems to be no easy way to get the outcome I want (which is not as expected). Is there no built-in function that exists in JS?



https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString


More From » javascript

 Answers
21

Try dividing the number of cents by 100 to get the dollar equivalent. I.E.:


const number = 1629;
const dollars = (cents / 100).toLocaleString("en-US", {style:"currency", currency:"USD"});

dollars now equals "$16.29"


[#64936] Tuesday, September 22, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
anyssaarielles

Total Points: 415
Total Questions: 107
Total Answers: 92

Location: Greenland
Member since Fri, Jul 31, 2020
4 Years ago
;