Forum Moderators: open

Message Too Old, No Replies

Number formats in Javascript

Need currency format if possible.

         

abbeyvet

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

WebmasterWorld Senior Member 10+ Year Member



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);
}

DrDoc

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

WebmasterWorld Senior Member 10+ Year Member



Try googling for "JavaScript sprintf" :)

abbeyvet

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

WebmasterWorld Senior Member 10+ Year Member



Thank you for that!

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

Glacai

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

10+ Year Member



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)



Google for "javascript number formatter" ;)