Numbers from a text box for calculation

  • Thread starter adam.waterfield
  • Start date
A

adam.waterfield

Hi guys,
I know this is a really basic problem, but I am buggered if I can
remember or find the soultion to it.

I have the two text boxes on a form, both of which a user enters a
number. I simple need to ADD THESE UP!

I know, its daft, but no matter what I do, it just adds them together.

Example: 1+1 is clearly 2, but it outputs 11!

An example of my code:

var loan = document.loandata.loan.value;
var other = document.loandata.other.value;

total = (loan+other);

Any help would be very much welcomed!

Adam
 
A

Amie

Hi,

You need to convert string to number. Use parseFloat(loan) or
parseInt(loan).

Hope it helps,
Amie
 
A

Arnaud Diederen

(e-mail address removed) writes:

Hi,

using the SpiderMonkey interpreter:

aundro@paddy:~$ js
js> (+ "1") + (+ "1")
2
js>
aundro@paddy:~$

Simply: the '+' unary operator, applied to a string, transforms it
into a number, if possible. (NaN otherwise)

Arnaud



Hi guys,
I know this is a really basic problem, but I am buggered if I can
remember or find the soultion to it.

I have the two text boxes on a form, both of which a user enters a
number. I simple need to ADD THESE UP!

I know, its daft, but no matter what I do, it just adds them together.

Example: 1+1 is clearly 2, but it outputs 11!

An example of my code:

var loan = document.loandata.loan.value;
var other = document.loandata.other.value;

total = (loan+other);

Any help would be very much welcomed!

Adam

--
Arnaud DIEDEREN
Software Developer
IONIC Software
Rue de Wallonie, 18 - 4460 Grace-Hollogne - Belgium
Tel: +32.4.3640364 - Fax: +32.4.2534737
mailto:[email protected]
http://www.ionicsoft.com
 
T

Thomas 'PointedEars' Lahn

Amie said:
You need to convert string to number. Use parseFloat(loan) or
parseInt(loan).

The `+' operator usually suffices. When integer values are required,
the base (e.g. 10) should be passed as second argument of parseInt().

Please read the FAQ <URL:http://jibbering.com/faq/> before you try to
provide advice here.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Amie said:
You need to convert string to number. Use parseFloat(loan) or
parseInt(loan).

The unary `+' operator usually suffices. When integer values are required,
the base (e.g. 10) should be passed as second argument of parseInt().

Please read the FAQ <URL:http://jibbering.com/faq/> before you try to
provide advice here.


PointedEars
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Tue, 7 Mar 2006 07:34:29 remote, seen in
news:comp.lang.javascript, (e-mail address removed) posted :
I know this is a really basic problem, but I am buggered if I can
remember or find the soultion to it.

Then you have evidently not searched competently, since the answer is
given clearly enough in the newsgroup FAQ, which all should read before
posting questions.

I have the two text boxes on a form, both of which a user enters a
number. I simple need to ADD THESE UP!

I know, its daft, but no matter what I do, it just adds them together.

That's what you want. Adding up and adding together have the same
meaning in English. What you get, however, is concatenation.

Example: 1+1 is clearly 2, but it outputs 11!

An example of my code:

var loan = document.loandata.loan.value;
var other = document.loandata.other.value;

total = (loan+other);

Parentheses superfluous.

The efficient way is to use the unary + operator. You could do
total = +loan + +other ;
but I'd use
var loan = +document.loandata.loan.value
var other = +document.loandata.other.value
total = loan + other




JRS: In article <[email protected]>,
dated Tue, 7 Mar 2006 07:47:41 remote, seen in
news:comp.lang.javascript said:
You need to convert string to number. Use parseFloat(loan) or
parseInt(loan).

Hope it helps,

One should also read the FAQ before answering, in order to avoid
demonstrating inadequate understanding.

Routine parseInt() should always be given two parameters, except when
using only one is necessary or provably safe. And it is only necessary
to use it if the numeric part of the input may be followed by non-
whitespace. Unary + is both brief and efficient; though in coursework
it may need explaining.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top