Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
31
rated 0 times [  36] [ 5]  / answers: 1 / hits: 131661  / 13 Years ago, wed, april 27, 2011, 12:00:00

I have number like this in my html



<div class=number>950000</div>


and I want jQuery change it to



<div class=number>Rp. 950.000</div>


How can I do that in jQUery?


More From » jquery

 Answers
12

Divide by 1000, and use .toFixed(3) to fix the number of decimal places.



var output = (input/1000).toFixed(3);


[EDIT]



The above solution only applies if the dot in the original question is for a decimal point. However the OP's comment below implies that it is intended as a thousands separator.



In this case, there isn't a single line solution (Javascript doesn't have it built in), but it can be achieved with a fairly short function.



A good example can be found here: http://www.mredkj.com/javascript/numberFormat.html#addcommas



Alternatively, a more complex string formatting function which mimics the printf() function from the C language can be found here: http://www.diveintojavascript.com/projects/javascript-sprintf


[#92530] Tuesday, April 26, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jackeline

Total Points: 245
Total Questions: 92
Total Answers: 100

Location: Ukraine
Member since Sun, Dec 13, 2020
4 Years ago
;