Skip to content Skip to sidebar Skip to footer

Add Comma Separator To A Value Variable

I have read some thousand comma separator JavaScript question/answer but found it hard to apply it in practice. For example I have the variable x = 1002387123498102989819826489712

Solution 1:

First of all, for such huge numbers you should use string format:

var x = "10023871234981029898198264897123897.231241235";

Otherwise, JavaScript will automatically convert it to exponential notation, i.e. 1.002387123498103e+34.

Then, according to the question about money formatting, you can use the following code:

x.replace(/(\d)(?=(\d{3})+\.)/g, "$1,");

It will result in: "10,023,871,234,981,029,898,198,264,897,123,897.231241235".

Post a Comment for "Add Comma Separator To A Value Variable"