const renderCommas = (number) => {
return number >= 1000 ? `${Number.parseFloat((number).toFixed(3))}` : number;
};
Asked
Active
Viewed 123 times
-3

Hunter McMillen
- 59,865
- 24
- 119
- 170

Rover33
- 1
-
`toFixed` should be only used for decimals, not for number localization. – Roko C. Buljan Aug 16 '18 at 17:24
-
what is your suggestion then – Rover33 Aug 16 '18 at 17:25
-
https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript – Hunter McMillen Aug 16 '18 at 17:26
-
1https://stackoverflow.com/q/2901102/438992 etc. It's usually worth searching before asking. – Dave Newton Aug 16 '18 at 17:26
-
@Rover33 Using some terms from my previous comment I was able to google `toLocaleString()` – Roko C. Buljan Aug 16 '18 at 17:27
-
Here is the MDN link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString – Arup Rakshit Aug 16 '18 at 17:29
-
3Meta: I don't think it's cool to downvote correct answers just to punish them for answering dupes. Just comment and move on; responsible users will delete their answers. – Dave Newton Aug 16 '18 at 17:29
-
1I agree with @DaveNewton. Downvote tells the answer is wrong. Like I felt. :) – Arup Rakshit Aug 16 '18 at 17:30
2 Answers
-3
You can use the following code to format a number with a comma:
var num = 100000;
var formatted = num.toLocaleString(undefined);
console.log(formatted);

Grant Miller
- 27,532
- 16
- 147
- 165

ryan
- 9
- 2
-
1I didn't downvote, but it's unclear to me why you're advocating for `undefined` as the parameter to `toLocaleString`, and I think without any explanation, it's an incomplete answer. – Dave Newton Aug 16 '18 at 20:03