Guest User

Untitled

a guest
Nov 16th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. function formatNumber(num) {
  2. if (!num || num == 'NaN') return '-';
  3. if (num == 'Infinity') return '∞';
  4. num = num.toString().replace(/$|,/g, '');
  5. if (isNaN(num))
  6. num = "0";
  7. sign = (num == (num = Math.abs(num)));
  8. num = Math.floor(num * 100 + 0.50000000001);
  9. cents = num % 100;
  10. num = Math.floor(num / 100).toString();
  11. if (cents < 10)
  12. cents = "0" + cents;
  13. for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3) ; i++)
  14. num = num.substring(0, num.length - (4 * i + 3)) + '.' + num.substring(num.length - (4 * i + 3));
  15. return (((sign) ? '' : '-') + num + ',' + cents);
  16. }
Add Comment
Please, Sign In to add comment