Closed Thread Icon

Topic awaiting preservation: parseInt Pages that link to <a href="https://ozoneasylum.com/backlink?for=8817" title="Pages that link to Topic awaiting preservation: parseInt" rel="nofollow" >Topic awaiting preservation: parseInt\

 
Author Thread
Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 08-15-2003 00:04

Q: What is the purpose of using parseInt? Let me explain.
When I calculate numeric things, I thought I always have to put parseInt
around variable. Otherwise it doesn't work properly when I add.
1+1=11. Something like that problem will happen without putting parseInt.

After while, I realised if I put parseInt, I got NaN in the output text
box when I didn't enter anything in the input text box. I wanted to
get 0 when I didn't enter the value. I deleted those parseInt things
from the calculation fomula. Then I got 0 when I didn't enter value in the input text field.

I am quite not sure why this happened.
Do you know what I mena???
Can you explain this?

Hiroki Kozai

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 08-15-2003 02:58

Ah, yes. Well, NaN stands for Not a Number, which means "I couldn't get a number out of this string." You can't safely omit the parseInt, however, because something like this:

form.input.value * 3

may evaluate to

"hello" * 3

which evaluates to NaN.

One way to do this is to see if the return value of parseInt is NaN, and if it is, replace the variable with zero, like so:

code:
var userinput = parseInt(form.input.value);
if (userinput == NaN)
userinput = 0;



That's basically what you want to do. However, that code won't work. NaN is a special value, unique in the fact that it is *not equal to itself*. (NaN == NaN is false.) To test if a value is NaN, you have to use the special isNaN function:

code:
var userinput = parseInt(form.input.value);
if (isNaN(userinput))
userinput = 0;



That should work.

Note that if you want to allow decimals, you should use parseFloat instead of parseInt.

There may be an easier way to do this than I've said, but this is a pretty thorough method.

Finally, note the importance of getting all user input ahead of time, before you start any computation, and putting the input into variables. If you carefully separate this part of your code from the part that does computation on the input, then it will be easier to work with. After the first part (reading the input with parseInt and all that), you know that what you have are valid numbers - and if they weren't valid numbers, you've either turned them into zero or given the user an error message and aborted. At this point, you know that you're only working with variables which you're certain are numbers, so you don't have to worry about anything going wrong, just as long as you write your program correctly.

Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 08-15-2003 04:52

Hi, Slime.
Many thanks for your reply.
I really appreciate that.
I have been trying to fix my code since then.
Could I ask a bit more?
Well, a friend of mine just told me a couple of minutes ago.
He said:

quote:
Why don't you give them default value 0?


After that I works fine!
What do you think of this way???

And I just wonder your code:

code:
var userinput = parseInt(form.input.value);
if (isNaN(userinput))
userinput = 0;



Don't I have to put { } around userinput=0???
I tried it with {} as well as without it.
Neither way doesn't work.... I am not sure why.
I will play with it once again though.




Hiroki Kozai

Hiroki
Paranoid (IV) Inmate

From: NZ
Insane since: Dec 2002

posted posted 08-15-2003 04:57

Yeah, I don't need {}.
I made small script to calculate.
I is running fine.
Many thanks.

Hiroki Kozai

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 08-15-2003 08:06

Yup, { } is optional for if's, else's, while's, for's, and pretty much everything except for switch's and function's when there's only one statement inside of it.

Sam
Bipolar (III) Inmate

From: Belgium
Insane since: Oct 2002

posted posted 08-27-2003 13:09

Don't forget the function parseInt() has two arguments!

parseInt('08') returns 0
parseInt('08',10) returns 8

It cost me four hours of debugging!

More info here

Regards,
Sam

« BackwardsOnwards »

Show Forum Drop Down Menu