Hello there i have problem in jQuery. Suppose i have a value like 20000. And i want the value like 20,000 that after two number a comma will be inserted. I can't do it using jQuery. Please anyone help me out.
Asked
Active
Viewed 354 times
-1
-
what if you'll have `345000`? what than? – Roko C. Buljan Aug 22 '13 at 23:25
-
Not sure why there is the php tag if you want a jQuery solution, but anyway if you are using PHP then [`number_format()`](http://php.net/manual/en/function.number-format.php) is the easiest solution IMO. – Fabrício Matté Aug 22 '13 at 23:25
-
Thanks to give me valuable links. – Subir Aug 22 '13 at 23:42
1 Answers
0
Since you said you want the comman inserted "after two numbers", I'm going to assume you have the easy case where you know that your input is a 5 digit number and don't want a more generic number formatting solution. In that case you can do something just like:
var num = 20000;
num = num.toString();
num = num.substr(0,2) + ',' + num.substr(2);
// Now num === "20,000"
Note that 20,000
will only read as twenty-thousand in certain locales. It could be read as just twenty in many locales, but 20000
will be read correctly by anyone who understands Arabic numerals.

Paul
- 139,544
- 27
- 275
- 264