Advertisement
Guest User

Untitled

a guest
Sep 19th, 2014
561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function CurrencyFormatted(amount)
  2. {
  3.     var i = parseFloat(amount);
  4.     if(isNaN(i)) { i = 0.00; }
  5.     var minus = '';
  6.     if(i < 0) { minus = '-'; }
  7.     i = Math.abs(i);
  8.     i = parseInt((i + .005) * 100);
  9.     i = i / 100;
  10.     s = new String(i);
  11.     if(s.indexOf('.') < 0) { s += '.00'; }
  12.     if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
  13.     s = minus + s;
  14.     return s;
  15. } // function CurrencyFormatted()
  16.  
  17. function CommaFormatted(amount)
  18. {
  19.     var delimiter = ",";
  20.     var a = amount.split('.',2)
  21.     var d = a[1];
  22.     var i = parseInt(a[0]);
  23.     if(isNaN(i)) { return ''; }
  24.     var minus = '';
  25.     if(i < 0) { minus = '-'; }
  26.     i = Math.abs(i);
  27.     var n = new String(i);
  28.     var a = [];
  29.     while(n.length > 3)
  30.     {
  31.         var nn = n.substr(n.length-3);
  32.         a.unshift(nn);
  33.         n = n.substr(0,n.length-3);
  34.     }
  35.     if(n.length > 0) { a.unshift(n); }
  36.     n = a.join(delimiter);
  37.     if(d.length < 1) { amount = n; }
  38.     else { amount = n + '.' + d; }
  39.     amount = minus + amount;
  40.     return amount;
  41. } // function CommaFormatted()
  42.  
  43.  
  44. function Update()
  45. {
  46.     var s = new String();
  47.     s = CurrencyFormatted(document.testing.amount.value); // Pass the value which you want to convert
  48.     s = CommaFormatted(s);  // Pass the value which you want to display as comma seperated
  49.     document.testing.result.value = s;
  50. } // function Update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement