Click to See Complete Forum and Search --> : Adding commas and trailing zero


DrLongGhost
08-14-2004, 09:06 PM
I've scoured the net for functions to add commas and found several. One worked for awhile then strangely stopped working for me. Others are set up to alter the content of a form and even though they work on the "example" form on the web page, I dont know how to pull out the code and make them into a function.

I found 2 great functions that would add commas to a number, stick a dollar sign in front and convert it to 2 decimal places, which is EXACTLY what I'm looking for. Unfortunately, once I started throwing different numbers at this function, I was able to get it to crash both IE and Navigator :-(

Bare bones, I need a function I can call on to add commas to a number.

Ideally, I'd like a function that I can call like this: convert(mynumber)

It would then take mynumber, add commas and possibly decimal places for use as currency:

If mynumber were 34555.7, I'd like it to return 34,555.70. If mynumber were 34555, I'd prefer it to return 34,555 but would definitely be satisfied if it returned 34,555.00.

Can anyone help me out with code for a function that A) actually works and B) will not crash my browser.

Thanks a bunch.

DrLongGhost
08-14-2004, 09:40 PM
Finally found a script that works!!!

I'll paste it below. I'm very pleased with this, but, if I may push my luck, can someone modify it for me, so it will convert 1234 to $1,234 instead of $1,234.00?

Not a huge hindrance to have the .00, but if I had my druthers, I'd nix it.

function FormatCurrency(num) {
var sign, cents;
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 = "0" + 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)?'':'-') + '$' + num + '.' + cents);
}

Charles
08-14-2004, 10:02 PM
<script type="text/javascript">

String.prototype.reverse = function () {return this.split('').reverse().join('')};

Number.prototype.withCommas = function () {
if (isNaN (this)) return NaN.toString();
s = Math.abs(this).toString().split('.');
return ['$', (this < 0 ? '-' : ''), s[0].reverse().match(/\d{1,3}/g).join(',').reverse(), s[1] ? '.' :'', s[1]].join('');
}

alert (Number(1234).withCommas())

</script>

Willy Duitt
08-14-2004, 10:06 PM
Here's one I use to format currency onkeyup. However, I was under the impression that you also wanted the number to be formatted to two decimal places which gets a little tricky when used as the user inputs the number...

Anyway, this should work for you....


<script type="text/javascript">
function format(input){
var num = input.value.replace(/\$|\,/g,'');
if(!isNaN(num)){
input.value = '$'+num.toString().split('').reverse().join('').replace(/(?=\d*\.?)(\d{3})/g,'$1,').split('').reverse().join('').replace(/^[\,]/,'');
}
else{ alert('You may enter only numbers in this field!');
input.value = input.value.replace(/[^\$\d\,\.]*$/g,'');
}
}
</script>
</head>

<body>
<form>
<input type="text" onkeyup="format(this)" onchange="format(this)">
</form>


......Willy

Charles
08-14-2004, 10:14 PM
Here's mine altered to fix the number to two decimal places but only if there is anything on the right sode of the '.'.

<script type="text/javascript">

String.prototype.reverse = function () {return this.split('').reverse().join('')};

Number.prototype.withCommas = function () {
if (isNaN (this)) return NaN.toString();
s = this == Math.floor(this) ? Math.abs(this).toString().split('.') : Math.abs(this).toFixed(2).split('.');
return ['$', (this < 0 ? '-' : ''), s[0].reverse().match(/\d{1,3}/g).join(',').reverse(), s[1] ? '.' :'', s[1]].join('');
}

alert (Number(1234).withCommas())
alert (Number(1234.1234).withCommas())

</script>

Willy Duitt
08-14-2004, 10:28 PM
Yea I think I broke mine when I removed the decimal part of the code...

Oooopss;
.....Willy