jQuery:货币格式编号 [英] jQuery: Currency Format Number
问题描述
我的html中有这样的数字
I have number like this in my html
<div class="number">950000</div>
我希望jQuery将其更改为
and I want jQuery change it to
<div class="number">Rp. 950.000</div>
我怎样才能在jquery中做到这一点?
How can I do that in jQUery?
推荐答案
除以1000,并使用 .toFixed(3)
来修正小数位数。
Divide by 1000, and use .toFixed(3)
to fix the number of decimal places.
var output = (input/1000).toFixed(3);
以上解决方案仅适用于原始问题中的点是小数点的情况。然而,下面的OP评论意味着它是一个千位分隔符。
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.
在这种情况下,没有单行解决方案(Javascript没有构建它in),但它可以通过一个相当短的函数来实现。
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.
这里可以找到一个很好的例子: http://www.mredkj.com/javascript/numberFormat.html#addcommas
A good example can be found here: http://www.mredkj.com/javascript/numberFormat.html#addcommas
或者,可以在此处找到更复杂的字符串格式化函数,该函数模仿C语言中的 printf()
函数: http://www.diveintojavascript.com/projects/javascript-sprintf
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
这篇关于jQuery:货币格式编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!