Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var CURRENCY_US = "$";
- var CURRENCY_EURO = "€";
- function testFormatNumbers() {
- var number = formatNumber(1234.56)
- var defcurr = formatCurrency(1234.56)
- var eurocurr = formatCurrency(1234.56, CURRENCY_EURO)
- alert(number + "\n" + defcurr + "\n" + eurocurr);
- }
- function formatNumber(num) {
- return _formatCurrency(num);
- }
- function formatCurrency(num, currencySign) {
- currencySign = currencySign || "$";
- return _formatCurrency(num, currencySign);
- }
- function _formatCurrency(num, currencySign) {
- currencySign = currencySign || "";
- if(currencySign !== "")
- currencySign = currencySign + " ";
- num = num.toString().replace(/\$|\,/g, '');
- if (isNaN(num))
- num = "0";
- sign = (num == (num = Math.abs(num)));
- num = Math.floor(num * 100 + 0.50000000001);
- cents = num % 100;
- num = Math.floor(num / 100).toString();
- if (cents < 10)
- cents = "";
- if (cents !== "")
- cents = "." + cents;
- for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
- num = num.substring(0, num.length - (4 * i + 3)) + ',' +
- num.substring(num.length - (4 * i + 3));
- return (((sign) ? '' : '(') + currencySign + "" + num + cents + ((sign) ? '' : ')'));
- }
Advertisement
Add Comment
Please, Sign In to add comment