用jQuery添加数字间距 [英] Adding digit spacing with jQuery
本文介绍了用jQuery添加数字间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想检查一个span元素并检查其数字格式,并使用jquery对它进行数字分组
I would like to inspect a span element and check for its number format and apply digit grouping to it with jquery
该数字有3个以上的数字,即> 1000,则必须进行数字分组,以将成千上万的数字与成千上万的数字分开:
It the number has more than 3 digits, that is for >1000, it must do digit grouping, to separate the thousands from the hundreds, and the hundred thousands from the thousands:
例如
<span>5500000.00</span>
应该成为
<span>5 500 000.00</span>
使用jQuery是否可能?
Is that possible with jQuery?
推荐答案
此任务有许多格式设置插件.或使用可以使用正则表达式.
There are number of formatting plugins for this task. Or use can use regular expression.
function formatPrice(price) {
return price.reverse().replace(/((?:\d{2})\d)/g, '$1 ').reverse();
}
// Need to extend String prototype for convinience
String.prototype.reverse = function() {
return this.split('').reverse().join('');
}
测试:
formatPrice('1234.00') // "1 234.00"
formatPrice('12345.00') // "12 345.00"
formatPrice('123456.00') // "123 456.00"
也许有一种方法可以在不还原字符串的情况下完成此操作?
这篇关于用jQuery添加数字间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文