Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Formating Numbers - Javascript

Status
Not open for further replies.

surfertide

Technical User
May 24, 2006
6
US
Hello:

I have this javascript code where I can find the percentage (2%, 2.5%, 3%) of a sales price. It works out fine but I would like to modify the code so when I type E.g: 500,000.00 the values would show up. At this moment it doesn't work with the format above. It only works if I type E.g: 500000

Is there a way to make it work if I put commas and a period for the decimals?
An example page could be found at:
13west.com/test/

Here's the javascript code:

<script Language="JavaScript">


<!--Hide From Other Browsers

function tips(form,changed){

var tip = form.tip.value;

if (changed == "tip"){
var two = tip * 0.02;
var twofive = (tip * 0.025);
var three = (tip * 0.03);

tip = formatCurrency(tip);
form.tip.value = tip;

}

form.two.value = formatCurrency(two);
form.twofive.value = formatCurrency(twofive);
form.three.value = formatCurrency(three);

}

function formatCurrency(num) // got most of this from a javascript example site
{
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);
}

//Stop Hiding -->


</script>
 
While using RegExp to strip the comma would be a better method, this should work. Replace this:

Code:
var tip = form.tip.value;

with this:

Code:
var tip = form.tip.value.splt(',').join('');

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I replace the var.tip with the one mentioned but still doesn't work.
After the change I test the file but with no results.
:-(
 
Well, if you copied and pasted BRPS's code without reading it or thinking on your own, you got a typo in it. Look through it and see if you can find the typo on your own.

This kind of thing is good practice for finding your OWN mistakes, and we all make them.

Lee
 
Found the typo! and it works now
Thank you very much to both of you.
I'm new at javascript I couldn't have caught it on my own.
Thanks again
:)
 
Aaah sorry - I didn't spot that one. Using the Firefox JS console would show you these sorts of things pretty much straight away, so using that instead of IE for development work is a good idea.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
It's good debugging practice for someone posting a question here. People get lazy, just copy and paste solutions they're given without really reading them and seeing what they do. Maybe we ought to make it more common to add minor typos to solutions provided to see if we can get those with questions to think a bit more. :)#

Lee
 
I sincerely hope you're just kidding!

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top