getting values from a form as Number, not a String

K

Kenny

I've run into this problem a couple of times. When I try to get a
value from a form text field and add them together like so…

a = document.myform.field1.value
b = document.myform.field2.value
c = a + b
document.myform.field3.value = c

It treats the values as a string and if the input numbers are 2 and 4
I get 24 and not 6 like I would expect.

I can do some crazy math to force it to recognize the numbers like
this…

c = (a*10/10) + (b*10/10)

and get the results I want, because when I use multiplication and
division, it recognizes the numbers as numbers. But dose anyone know
how to get it to recognizes the numbers in the first place.

Thanks
Kenny
 
J

Jerry Park

Kenny said:
I've run into this problem a couple of times. When I try to get a
value from a form text field and add them together like so…

a = document.myform.field1.value
b = document.myform.field2.value
c = a + b
document.myform.field3.value = c

It treats the values as a string and if the input numbers are 2 and 4
I get 24 and not 6 like I would expect.

I can do some crazy math to force it to recognize the numbers like
this…

c = (a*10/10) + (b*10/10)

and get the results I want, because when I use multiplication and
division, it recognizes the numbers as numbers. But dose anyone know
how to get it to recognizes the numbers in the first place.

Thanks
Kenny
All field values are string values.

Any math operator except the '+' operator should cause conversion to a
numeric type.

parseFloat() and parseInt() can also explicitly convert a string to a
number.

var a * 1 or var a - 0 quickly causes conversion without the overhead of
a function call.
 
T

Thomas 'PointedEars' Lahn

Jerry said:
var a * 1 or var a - 0 quickly causes conversion without the overhead of
a function call.

You mean a * 1 and a - 0, and +a is quicker :)


PointedEars
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen
It treats the values as a string and if the input numbers are 2 and 4
I get 24 and not 6 like I would expect.
But dose anyone know
how to get it to recognizes the numbers in the first place.

Yes, anyone who has read the FAQ should know.

One should always seek and read a relevant FAQ before posting to News;
it often gets the answer quicker, and it saves annoyance.

JP's response has been incompletely corrected by PE; see FAQ 4.12.
 
T

Thomas 'PointedEars' Lahn

Dr said:
Kenny said:
It treats the values as a string and if the input numbers are 2 and 4
I get 24 and not 6 like I would expect.
But dose anyone know
how to get it to recognizes the numbers in the first place.

[...]
JP's response has been incompletely corrected by PE; see FAQ 4.12.

The FAQ entry does not imply in any way that my correction
is wrong as test results posted in other threads have proven.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Dr said:
Kenny said:
It treats the values as a string and if the input numbers are 2 and
4 I get 24 and not 6 like I would expect.
But dose anyone know how to get it to recognizes the numbers in the
first place.

[...]
JP's response has been incompletely corrected by PE; see FAQ 4.12.

The FAQ entry does not imply in any way that my correction is
wrong (incl. not incomplete) as test results posted in other
threads have proven.


PointedEars
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
Thomas 'PointedEars' Lahn
Dr said:
Kenny said:
It treats the values as a string and if the input numbers are 2 and
4 I get 24 and not 6 like I would expect.
But dose anyone know how to get it to recognizes the numbers in the
first place.

[...]
JP's response has been incompletely corrected by PE; see FAQ 4.12.

The FAQ entry does not imply in any way that my correction is
wrong (incl. not incomplete) as test results posted in other
threads have proven.

Yes, it does so imply. You did not correct JP's omission in not stating
that parseInt almost always needs a second parameter of 10. The FAQ
stresses that.

One should recommend reading the FAQ to those who ask rather well-known
questions at every reasonable possibility; doing so may render possible
subsequent questions unnecessary.

If you had looked at FAQ 4.12 yourself, you should have realised that it
deals with only parseInt; and your response ignored parseInt.
 
T

Thomas 'PointedEars' Lahn

Dr said:
Dr said:
[...]
JP's response has been incompletely corrected by PE; see FAQ 4.12.

The FAQ entry does not imply in any way that my correction is
wrong (incl. not incomplete) as test results posted in other
threads have proven.

Yes, it does so imply. You did not correct JP's omission in not stating
that parseInt almost always needs a second parameter of 10.

In my posting did not refer to his usage of parseInt(...) at all
but only referred to his way of accomplishing conversion without
any function. My correction is therefore not incomplete.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top