Closed Thread Icon

Topic awaiting preservation: how do i calculate input values Pages that link to <a href="https://ozoneasylum.com/backlink?for=8256" title="Pages that link to Topic awaiting preservation: how do i calculate input values" rel="nofollow" >Topic awaiting preservation: how do i calculate input values\

 
Author Thread
GRUMBLE
Paranoid (IV) Mad Scientist

From: Omicron Persei 8
Insane since: Oct 2000

posted posted 08-08-2002 19:27

when i want to calculate values from inputs they are calculated as if they were strings.
how can i mathematically calculate them?

form.input1.value+form.input2.value=real mathematical result

bitdamaged
Maniac (V) Mad Scientist

From: 100101010011 <-- right about here
Insane since: Mar 2000

posted posted 08-08-2002 19:45

you need to typecast them as numbers

Number(form.input1.value) + Number(form.input2.value) = something;



.:[ Never resist a perfect moment ]:.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 08-08-2002 21:12

You might also want to try the parseInt and parseFloat functions, or you could call the .valueOf method of the strings, as so:

parseInt(form.input1.value)+parseInt(form.input2.value)
parseFloat(form.input1.value)+parseFloat(form.input2.value)
form.input1.value.valueOf()+form.input2.value.valueOf()

You may even want to use the eval() function. This would allow the user to type in something like "3+4*5" and it would be evaluated. Of course, they could also type in some illegal javascript code, which could cause an error, so it's up to you.

Personally, I'd suggest parseInt or parseFloat since they're meant for this sort of thing.

GRUMBLE
Paranoid (IV) Mad Scientist

From: Omicron Persei 8
Insane since: Oct 2000

posted posted 08-09-2002 11:53

thank you very much, guys!

GRUMBLE
Paranoid (IV) Mad Scientist

From: Omicron Persei 8
Insane since: Oct 2000

posted posted 08-21-2002 11:10

just wanted to add that valueOf() doesnt really work correctly!

if you type 14 i just returns 1.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 08-21-2002 17:38

Actually, now that I think about it, it shouldn't work correctly. So forget that one.

Genevieve
Paranoid (IV) Inmate

From: Santa Clara, CA, USA
Insane since: Jul 2002

posted posted 08-21-2002 17:50

You know, I've done this w/out casting them as numbers...but as vars. I wonder why it worked for me. I did:

code:
var age = prompt("What is your age?", " ");
if (age == " ")
{
var age = 0;
}

var days = 365 * age;
var hours = 24 * days;
var min = 60 * hours;
var sec = 60 * min;

document.writeln("Wowzers! You're " + sec + " old!!!");



and it calculated it correctly... Anyone know why it would work this way and not in the other?

Genevieve Hokanson
Student Intern, GPB http://einstein.stanford.edu http://www.geocities.com/genevievescu/

[edit: Forgot semicolons ";" in the var's hehe]

[This message has been edited by Genevieve (edited 08-21-2002).]

meccaman
Nervous Wreck (II) Inmate

From:
Insane since: Aug 2002

posted posted 08-21-2002 23:56

Maybe bc the + operator is an arthimetic operator and a string operator, where as the * operator is just an arthimetic operator.
When using the * operator, the string value is implicitly treated as a number when multiplied by another number.
However, when using the + operator, you must explicitly convert a string representing a number to a number type if you want to add it to a number and get the sum, otherwise the string + operator takes precedence and will concatenate the number to the string, resulting in a new string.

i.e.

6 * "5" = 30;
6 + "5" = "65";
6 + parseInt("5") = 11;


Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 08-22-2002 03:46

Exactly =)

Genevieve
Paranoid (IV) Inmate

From: Santa Clara, CA, USA
Insane since: Jul 2002

posted posted 08-22-2002 17:05

Ah. Interesting to know for future reference. Thanks!

(LOL. I wasn't going to name them vars but it's been over a year since I took C and I couldn't remember what numbers were called. hehehe. I only knew it wasn't "number" lol. Thanks for that as well! ^_^)

Genevieve Hokanson
Student Intern, GPB
http://einstein.stanford.edu
http://www.geocities.com/genevievescu/

« BackwardsOnwards »

Show Forum Drop Down Menu