jQuery计算(获取NaN,但仅当数量超过1000时)

时间:2016-01-21 09:35:42

标签: jquery

请听我说,这是我遇到的一个奇怪的错误。让我们说discountValue = 5grandTotal = 10discountShow将正确输出并格式化结果。但如果grandTotalgreater而不是999discountShow将输出NaN

function discount_compute() {
  var discountValue = parseFloat($('#discount').val().replace(',', '.'));
  var discountTotal = grandTotal * (1 - (discountValue / 100));
  var discountShow = numeral(discountTotal.toFixed(2)).format('0,0.00'); 
  $('.net-total-discount').html(discountShow + " €");
  }

1 个答案:

答案 0 :(得分:0)

你可以试试这个。

format('0,0.00');更改为format('0,0.[00]');

<强> JsFiddle Demo

&#13;
&#13;
function discount_compute() {
  var grandTotal = 10;
  var discountValue = 5;
  var discountTotal = grandTotal * (1 - (discountValue / 100));
  var discountShow = discountTotal.toFixed(2)
  var discountShow = numeral(discountTotal.toFixed(2)).format('0,0.[00]'); 
  document.write(discountShow + " &euro;");
}
discount_compute();
&#13;
<script src="//cdnjs.cloudflare.com/ajax/libs/numeral.js/1.4.5/numeral.min.js"></script>
&#13;
&#13;
&#13;