Welcome to WebmasterWorld Guest from 18.208.187.169

Forum Moderators: open

Message Too Old, No Replies

Number formats in Javascript

Need currency format if possible.

     
3:34 pm on Nov 17, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Aug 29, 2003
posts:944
votes: 0


Don't even know if what I want is possible, and am a JavaScript Dummy (note capital D) so cannot even start to figure it out.

I am using a JavaScript (cobbled together from various other scripts, a bit messy but it works) to add/subtract/multipy some values entered into a form.

These are finacial figures, and so there is every chance that people will enter the values as, say, 30,000 rather than 30000. At them moment if they do the form cannot deal with it.

Is it possible to add figures entered in this format?

Or to have it so that it corrects to that format if they don't (or even vice versa)?

Also I am passing results from the form to another page where they are displayed - and would really like them to display with the commas.

Currently the script is using this to round figures:

function round(number,X) {
X = (!X? 2 : X);
return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}

3:51 pm on Nov 17, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 15, 2002
posts:6807
votes: 0


Try googling for "JavaScript sprintf" :)
4:15 pm on Nov 17, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Aug 29, 2003
posts:944
votes: 0


Thank you for that!

Now I have a headache - I have learned something though: It can be done, just not by me :)

6:15 pm on Nov 17, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Oct 22, 2002
posts:71
votes: 0


My javascript is not too hot, but maybe if you convert the number to a string or pass it as a string, you could loop through the number removing unwanted chars, something like

var s = "";
for(var i=0; i<number.length; i++)
{
if((number.charAt(i) >= '0' && number.charAt(i) <= '9') number.charAt(i) == '.') s += number.charAt(i);
}
var newnum = parseFloat(s);

dcrombie

1:49 pm on Nov 18, 2003 (gmt 0)

Inactive Member
Account Expired

 
 


Google for "javascript number formatter" ;)