Velocity Reviews > Numbers from a text box for calculation

# Numbers from a text box for calculation

Guest
Posts: n/a

 03-07-2006
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!

Amie
Guest
Posts: n/a

 03-07-2006
Hi,

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

Hope it helps,
Amie

Arnaud Diederen
Guest
Posts: n/a

 03-07-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:

Hi,

using the SpiderMonkey interpreter:

js> (+ "1") + (+ "1")
2
js>

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!
>
>

--
Arnaud DIEDEREN
Software Developer
IONIC Software
Rue de Wallonie, 18 - 4460 Grace-Hollogne - Belgium
Tel: +32.4.3640364 - Fax: +32.4.2534737
(E-Mail Removed)
http://www.ionicsoft.com

Thomas 'PointedEars' Lahn
Guest
Posts: n/a

 03-07-2006
Amie wrote:

> 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().

PointedEars

Thomas 'PointedEars' Lahn
Guest
Posts: n/a

 03-07-2006
Amie wrote:

> 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().

PointedEars

Dr John Stockton
Guest
Posts: n/a

 03-07-2006
JRS: In article <(E-Mail Removed). com>,
dated Tue, 7 Mar 2006 07:34:29 remote, seen in
news:comp.lang.javascript, (E-Mail 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 <(E-Mail Removed). com>,
dated Tue, 7 Mar 2006 07:47:41 remote, seen in
news:comp.lang.javascript, Amie <(E-Mail Removed)> posted :

>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

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.

--
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Gnarlodious HTML 4 05-05-2010 11:30 AM Petyr David Javascript 0 08-29-2007 12:27 PM Neredbojias HTML 3 10-04-2005 12:55 AM Mariame ASP .Net 2 08-19-2004 09:37 AM grayFalcon Javascript 2 02-16-2004 11:20 AM