简体   繁体   English

jQuery-数字格式问题

[英]jquery - number formatting issue

This is my code: 这是我的代码:

<script src="js/jquery.js" type="text/javascript"></script>
<script src="js/jquery.field.js" type="text/javascript"></script>
<script src="js/jquery.calculation.js" type="text/javascript"></script>    
<script>    
   $(function() {
      $('input[name^=sum]').keyup(function() {
            var sum1 = parseFloat($('input[name=sum1]').val()) || 0;
            var sum2 = parseFloat($('input[name=sum2]').val()) || 0; 
            var sum3 = parseFloat($('input[name=sum3]').val()) || 0; 

            $('#c_card_amount').val(sum1*sum3/100);        
            $('#total_inc_charges').val((sum1*sum3/100)+sum1);
       });
   });
</script>

In the input fields I would like to show the numbers in 1,000.00 format. 在输入字段中,我想以1,000.00格式显示数字。

How can I format the numbers like this? 我该如何格式化数字? Right now after the calculation numbers sometimes gets messed up like 452.25600000001. 现在,在计算之后,数字有时会像452.25600000001那样混乱。 I would like to avoid that as well. 我也想避免这种情况。 Would be nice to rule the numbers properly. 最好正确地控制数字。

Thank you for your help. 谢谢您的帮助。

I recommend that you use NumberFormatter . 我建议您使用NumberFormatter Also you can see this question . 您也可以看到这个问题

I would recommend you to try jQuery Format as mentioned in this question: Format numbers in javascript . 我建议您尝试使用此问题中提到的jQuery格式javascript中的数字格式

Below is an example of jQuery Format: 以下是jQuery格式的示例:

$.format.locale({
    number: {
        groupingSeparator: ',',
        decimalSeparator: '.'
    }
});
$.format.number(1000, '#,###.00');    // result: 1,000.00
$.format.number(452.25600000001, '#,###.00');    // result: 452.26

I would recommend you to use javascipt instead of jQuery. 我建议您使用javascipt代替jQuery。 While jQuery made life more easy but don't forget that jQuery can be too large and your user have to load it on page while browsing. 尽管jQuery使生活更轻松,但不要忘记jQuery可能太大,并且用户必须在浏览时将其加载到页面上。

Read this article for your answer http://www.mredkj.com/javascript/numberFormat.html 阅读本文以获取答案http://www.mredkj.com/javascript/numberFormat.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM