Jes Sie

Jes Sie

  • 744
  • 1.2k
  • 282.2k

Number Formatting in JQuery

Jul 15 2019 2:46 AM
Good day to all, 
 
I just started developing a program in MVC (very newbie in both MVC and JQuery). In my front-end, I need to add two textbox and show the result in the 3rd textbox. Below is my JQuery code:
 
  1. function Comma(Num) {        
  2.         Num += '';  
  3.         Num = Num.replace(','''); Num = Num.replace(','''); Num = Num.replace(',''');  
  4.         Num = Num.replace(','''); Num = Num.replace(','''); Num = Num.replace(',''');  
  5.         x = Num.split('.');  
  6.         x1 = x[0];  
  7.         x2 = x.length > 1 ? '.' + x[1] : '';  
  8.         var rgx = /(\d+)(\d{3})/;  
  9.         while (rgx.test(x1))  
  10.             x1 = x1.replace(rgx, '$1' + ',' + '$2');  
  11.         return x1 + x2;  
  12.     }  
  13.   
  14.     $(function () {  
  15.   
  16.         var total_amount = function () {  
  17.             var sum = 0;  
  18.             $('.amount').each(function () {  
  19.                 var num = $(this).val().replace(',''');  
  20.                 if (num != 0) {  
  21.                     sum += parseFloat(num);  
  22.                 }  
  23.             });  
  24.             $('#total_amount').val(sum);  
  25.         }  
  26.   
  27.   
  28.         $('.amount').keyup(function () {  
  29.             total_amount();  
  30.         });  
  31.     });  
 Below is my mark-up also:
 
  1. @Html.LabelFor(model => model.laborCost, htmlAttributes: new { @class = "control-label col-md-1" })  
  2.                             <div class="col-md-2">  
  3.                                 @Html.EditorFor(model => model.laborCost, new { htmlAttributes = new { @class = "form-control text-right amount", @onkeyup = "javascript:this.value=Comma(this.value);" } })  
  4.                                 @Html.ValidationMessageFor(model => model.laborCost, "", new { @class = "text-danger" })  
  5.                             </div>  
  6.                             @Html.LabelFor(model => model.partsCost, htmlAttributes: new { @class = "control-label col-md-1" })  
  7.                             <div class="col-md-2">  
  8.                                 @Html.EditorFor(model => model.partsCost, new { htmlAttributes = new { @class = "form-control text-right amount", @onkeyup = "javascript:this.value=Comma(this.value);" } })  
  9.                                 @Html.ValidationMessageFor(model => model.partsCost, "", new { @class = "text-danger" })  
  10.                             </div>  
  11.                             <div class="col-sm-1">  
  12.                                 <b>  
  13.                                     Total  
  14.                                 </b>  
  15.                             </div>  
  16.                             <div class="col-sm-2">  
  17.                                 <input type="text" name="totalCost" class="form-control text-right" readonly="readonly" value="0" id="total_amount" />  
  18.                             </div>  
My problem is that, whenever the number goes more than 6 0's,  the result is no long correct. see sample:
 
 
 My country is Laos so transaction amount usually exceeds a million. Thanks a lot for any assistance.
 
 
 
 
 

Answers (3)