JavaScript的;如何在三位数后设置点?

时间:2017-03-24 05:53:17

标签: javascript jquery numbers price

我有以下JavaScript代码:

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(".",","))

这将计算一个数字,将.更改为,并将其四舍五入为逗号后的两位数。

我需要做的是在树数字之前添加一个点。

意思是:

1.234,56代替1234,56

1.234.567,89代替1234567,89

有人知道这样做的方法吗?

5 个答案:

答案 0 :(得分:2)

您应该使用带有正则表达式的附加String#replace(以匹配,之前的三组中的数字)和替换函数(以便为每个匹配添加.)。

您更新的代码:

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')

function format(n) {
  return n.toFixed(2).replace('.', ',').replace(/\d{3}(?=(\d{3})*,)/g, function(s) {
    return '.' + s
  })
}

$('#calculation').text(format(calculation))

<小时/>

演示实施:

var calculations = [
  1234.56,
  1234567.89,
  1234,
  .12
]

function format (n) {
  return n.toFixed(2).replace('.', ',').replace(/\d{3}(?=(\d{3})*,)/g, function (s) {
    return '.' + s
  })
}

console.log(calculations.map(format))

答案 1 :(得分:1)

ECMAScript Internationalization API有一个数字格式功能。 Intl.NumberFormat()

您可以使用纯javascript:

这样使用它

var calculation = document.getElementById('money').value;

//1st way
var moneyFormatter  = new Intl.NumberFormat();
document.getElementById('formattedMoney').innerText = moneyFormatter.format(calculation);

// 2nd way, using currency
var moneyFormatter2 = new Intl.NumberFormat('en-US', {
  style: 'currency',
  currency: 'USD',
  minimumFractionDigits: 2
});

document.getElementById('formattedMoney2').innerText = moneyFormatter2.format(calculation);
<label>Calculation:</label> <input type="text" value="123456789.25" name="money" id="money" />

<ul>
  <li>Number Format: <b><span id="formattedMoney"></span></b><br/><br/></li>
  <li>Currency Format: <b><span id="formattedMoney2"></span><b/></li>
</ul>

IE11,Firefox和Chrome支持它,但我不确定其他浏览器。

http://www.ecma-international.org/ecma-402/1.0/#sec-11.1.2

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat

答案 2 :(得分:1)

在现代浏览器(Chrome 24 +,Firefox 29 +,IE11)上,您可以使用Number.prototype.toLocaleString()

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString

var number = 3500.12;
console.log(number.toLocaleString());

这使用浏览器的区域设置自动格式化数字。我的浏览器设置为德语,因此输出为

3.500,12

如果具有不同语言环境的人使用您的网站,他们会看到他们期望的数字。

答案 3 :(得分:0)

此功能适用于您:

function formatNumber(num) {
  var first = num.split(',');
  var digits = first[0].split('').reverse();
  var new_digits = [];
  for(var i =0; i<digits.length; i++) {
    if((i+1)%3==0) {
      new_digits.push(digits[i]);
      new_digits.push('.');
    }
    else {
      new_digits.push(digits[i]);
    }
  }
  var new_num = new_digits.reverse().join("")+','+first[1];
  return new_num;
}

答案 4 :(得分:0)

此代码对我有用:

<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>

此代码将:

  1. 在dezimal之后舍入(用于业务)到两位数。
  2. 将小数点更改为逗号。
  3. 在十进制前每三位设置一个点。