By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,612 Members | 1,632 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,612 IT Pros & Developers. It's quick & easy.

getting values from a form as Number, not a String

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Kenny wrote:
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.

Jul 20 '05 #2

P: n/a
Jerry Park wrote:
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
Jul 20 '05 #3

P: n/a
Thomas 'PointedEars' Lahn wrote:
Jerry Park wrote:

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

OK

Jul 20 '05 #4

P: n/a
JRS: In article <3d*************************@posting.google.com> , seen
in news:comp.lang.javascript, Kenny <ke*********@email.com> posted at
Wed, 26 Nov 2003 11:10:04 :-
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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #5

P: n/a
Dr John Stockton wrote:
Kenny <ke*********@email.com> wrote:
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
Jul 20 '05 #6

P: n/a
Dr John Stockton wrote:
Kenny <ke*********@email.com> wrote:
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
Jul 20 '05 #7

P: n/a
JRS: In article <3F************@PointedEars.de>, seen in
news:comp.lang.javascript, Thomas 'PointedEars' Lahn
<Po*********@web.de> posted at Tue, 2 Dec 2003 16:51:26 :-
Dr John Stockton wrote:
Kenny <ke*********@email.com> wrote:
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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #8

P: n/a
Dr John Stockton wrote:
news:comp.lang.javascript, Thomas 'PointedEars' Lahn
Dr John Stockton wrote:
[...]
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
Jul 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.