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?
On the Home tab, click the Dialog Box Launcher next to Number. Tip: You can also press Ctrl+1 to open the Format Cells dialog box. In the Format Cells dialog box, in the Category list, click Currency or Accounting. In the Symbol box, click the currency symbol that you want.
JavaScript numbers can be formatted in different ways like commas, currency, etc. You can use the toFixed() method to format the number with decimal points, and the toLocaleString() method to format the number with commas and Intl. NumberFormat() method to format the number with currency.
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
Here is the cool regex style for digit grouping:
thenumber.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1.");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With