Click to See Complete Forum and Search --> : Formatting Text Field as Currency


kelemvor
03-12-2003, 11:35 AM
Is there an easy command that will take a number in a variable and format it in currency with a $ and 0's after the decimal or would I ahve to do it manually in a string?

If I have to do it manually, does anyone have a generic function that I can borrow? :)

Thanks.

AdamBrill
03-12-2003, 12:23 PM
Try this:
function addDecimal(string){
if(!/^\$/i.test(string)){
string = "$"+string;
}
if(!/^\$[0-9]*\./i.test(string)){
string+=".00";
}
while(!/^\$[0-9]*\.[0-9]{2}/i.test(string)){
string+="0";
}
return string;
}Let me know if you have any problems...

Dan Drillich
03-12-2003, 09:24 PM
If you want to take care of numbers like 225.224, the following will round and truncate them -


function addDecimal(string){

var exp = "^([0-9]*).([0-9])([0-9])([0-9]+)$";
var result = string.match(exp);

if (result) {
var t = result[2] + result[3] + "." + result[4];
var r = Math.round(t);
string = result[1] + "." + r;
}

if(!/^\$/i.test(string)){
string = "$"+string;
}
if(!/^\$[0-9]*\./i.test(string)){
string+=".00";
}
while(!/^\$[0-9]*\.[0-9]{2}/i.test(string)){
string+="0";
}
return string;
}

AdamBrill
03-12-2003, 09:46 PM
Good point, Dan. I forgot all about that. :) Actually, I also forgot to check if there were any characters besides numbers in the string... This function should be closer:function addDecimal(string){
if(!/[a-z]/gi.test(string)){
if(!/\w*\.[0-9]{,2}/i.test(string)){
string=Math.round(Number(string)*100)/100;
}
if(!/^\$/i.test(string)){
string = "$"+string;
}
if(!/^\$[0-9]*\./i.test(string)){
string+=".00";
}
while(!/^\$[0-9]*\.[0-9]{2,}/i.test(string)){
string+="0";
}
return string;
}else{
return false;
}
}Just so you know, if there are any characters beside numbers in the string it will return false.

Nedals
03-12-2003, 09:50 PM
How about this..

dollars = '$' + val.toFixed(2);

AdamBrill
03-12-2003, 09:56 PM
Nedals - That would work except that it would cause an error if they do it to a string. So, if they do something like $20 it won't work. That is why I made it like I did...

Nedals
03-12-2003, 10:15 PM
OK, adam, find the hole in this :)

amt = amt.replace(/^[^\d]/,'')
dollars = '$'+(parseFloat(amt)).toFixed(2)

Just for fun.. :D

AdamBrill
03-12-2003, 10:55 PM
Originally posted by nedals
OK, adam, find the hole in this :)Ok, but just remember that you asked for it... I normally wouldn't be this picky. :D

Try these:

1. 4 dollars and 6 cents
2. $4o3
3. $4.*

And I'm sure I missed something... ;) But, I must admit, your code works pretty much as good as mine and it is MUCH shorter... To tell you the truth, I forgot about the toFixed command and am pretty mad at myself for forgetting about it... LOL

Nedals
03-12-2003, 11:23 PM
If you can't have fun, what's the point. :D

Jona
03-12-2003, 11:24 PM
Tsk, tsk... Programming = fun!!!

Just my two cents... ;)

kelemvor
03-12-2003, 11:26 PM
Glad you guys are having fun but I jsut went with the first suggestion even though I don't know what the whoe/^\[ stuff is. :)

I'm just prompting the user for a number and doing some math so there's no need for me to worry about all that other stuff but it's interesting to know.