Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 31st, 2012  |  syntax: None  |  size: 1.04 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How do I amend an existing javascript to display currency values including cents?
  2. function CurrencyFormatted(amount)
  3. {
  4.     var i = parseFloat(amount);
  5.     if(isNaN(i)) { i = 0.00; }
  6.     var minus = '';
  7.     if(i < 0) { minus = '-'; }
  8.     i = Math.abs(i);
  9.     i = parseInt((i + .005) * 100);
  10.     i = i / 100;
  11.     s = new String(i);
  12.     if(s.indexOf('.') < 0) { s += '.00'; }
  13.     if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
  14.     s = minus + s;
  15.     return s;
  16. }
  17.        
  18. var n = 1.2345;
  19. n.toFixed(2); // "1.23"
  20.        
  21. (1.234).toFixed(2); // "1.23"
  22. (1.235).toFixed(2); // "1.24"
  23.        
  24. function CurrencyFormatted(amount)
  25. {
  26.     return parseFloat(amount).toFixed(2);
  27. }
  28.        
  29. function CurrencyFormatted(amount)
  30. {
  31.     return (parseFloat(amount) + 0.005).toFixed(2);
  32. }
  33.        
  34. <?php
  35. $value = 5;
  36.  
  37. setlocale(LC_MONETARY, 'en_US');
  38. echo '$'.money_format('%i', $value) . "n";//$5.00
  39.  
  40.  
  41. $value = 5.545;
  42.  
  43. setlocale(LC_MONETARY, 'en_US');
  44. echo '$'.money_format('%i', $value) . "n";//$5.54
  45.  
  46. $value = 9.99;
  47.  
  48. setlocale(LC_MONETARY, 'en_US');
  49. echo '$'.money_format('%i', $value) . "n";//$9.99
  50.  
  51. ?>