1

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.

How can I format the numbers like this? Right now after the calculation numbers sometimes gets messed up like 452.25600000001. I would like to avoid that as well. Would be nice to rule the numbers properly.

Thank you for your help.

Andreas Köberle
  • 106,652
  • 57
  • 273
  • 297
Peter
  • 135
  • 1
  • 5
  • 13

3 Answers3

2

I recommend that you use NumberFormatter. Also you can see this question.

Community
  • 1
  • 1
Saeed Neamati
  • 35,341
  • 41
  • 136
  • 188
2

I would recommend you to try jQuery Format as mentioned in this question: Format numbers in javascript.

Below is an example of jQuery Format:

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

I would recommend you to use javascipt instead of 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.

Read this article for your answer http://www.mredkj.com/javascript/numberFormat.html

RAJ
  • 9,697
  • 1
  • 33
  • 63