Jes Sie

Jes Sie

  • 701
  • 1.2k
  • 264.5k

Format Currency with comma and decimal

Jun 25 2020 1:31 AM
Hello everyone, I need your assistance. I need to format a numeric value to contain comma and decimal. I found this jquery (below) but it does not have a decimal. Can someone help me with how to add a decimal place? thank you.
 
  1. function FormatCurrency(ctrl) {  
  2.     //Check if arrow keys are pressed - we want to allow navigation around textbox using arrow keys  
  3.     if (event.keyCode == 37 || event.keyCode == 38 || event.keyCode == 39 || event.keyCode == 40) {  
  4.         return;  
  5.     }  
  6.   
  7.     var val = ctrl.value;  
  8.   
  9.     val = val.replace(/,/g, "")  
  10.     ctrl.value = "";  
  11.     val += '';  
  12.     x = val.split('.');  
  13.     x1 = x[0];  
  14.     x2 = x.length > 1 ? '.' + x[1] : '';  
  15.   
  16.     var rgx = /(\d+)(\d{3})/;  
  17.   
  18.     while (rgx.test(x1)) {  
  19.         x1 = x1.replace(rgx, '$1' + ',' + '$2');  
  20.     }  
  21.   
  22.     ctrl.value = x1 + x2;  
  23. }  
  24.   
  25. function CheckNumeric() {  
  26.     return event.keyCode >= 48 && event.keyCode <= 57;  
  27. }  
 
 
 

Answers (1)