JavaScript;三位数后如何设置点? [英] JavaScript; How to set dot after three digits?
本文介绍了JavaScript;三位数后如何设置点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下JavaScript代码:
I have the following JavaScript code:
var calculation = $('select[name=somevalue1] option:selected').data('calc')-($('select[name=somevalue1] option:selected').data('calc')/100*$('select[name=somevalue2] option:selected').data('calc'))-($('select[name=somevalue1] option:selected').data('calc')-($('select[name=somevalue1] option:selected').data('calc')/100*$('select[name=somevalue2] option:selected').data('calc')))/100*$('select[name=somevalue3] option:selected').data('calc');
$('#calculation').text((calculation).toFixed(2).replace(".",","))
这将计算一个数字,将.
更改为,
,并将其四舍五入为逗号后的两位数.
That will calculate a number, change .
to ,
and round it to two digits after comma.
我需要做的是在树的数字之后添加一个点.
What I need to do is to add a dot after tree digits before come.
手段:
1.234,56
而不是1234,56
1.234.567,89
而不是1234567,89
有人知道这样做的方法吗?
Does anybody know a way to do that?
推荐答案
此代码对我有用:
<p id="calculation"></p>
<script>
function numberWithCommas(x) {
var parts = x.toString().split(".");
parts[0]=parts[0].replace(/\B(?=(\d{3})+(?!\d))/g,".");
return parts.join(",");
}
var calculation = 1231739216.34367;
document.getElementById("calculation").innerHTML = numberWithCommas((calculation).toFixed(2))
</script>
此代码将:
- 小数点后两位(用于商务).
- 将小数点更改为逗号.
- 在小数点前每三位数设置一个点.
这篇关于JavaScript;三位数后如何设置点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文