Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   Numbers from a text box for calculation (http://www.velocityreviews.com/forums/t923411-numbers-from-a-text-box-for-calculation.html)

 adam.waterfield@gmail.com 03-07-2006 03:34 PM

Numbers from a text box for calculation

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 03-07-2006 03:47 PM

Re: Numbers from a text box for calculation

Hi,

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

Hope it helps,
Amie

 Arnaud Diederen 03-07-2006 03:56 PM

Re: Numbers from a text box for calculation

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
http://www.ionicsoft.com

 Thomas 'PointedEars' Lahn 03-07-2006 07:22 PM

Re: Numbers from a text box for calculation

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

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

PointedEars

 Thomas 'PointedEars' Lahn 03-07-2006 07:24 PM

Re: Numbers from a text box for calculation

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

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

PointedEars

 Dr John Stockton 03-07-2006 10:13 PM

Re: Numbers from a text box for calculation

JRS: In article <1141745669.452980.99630@v46g2000cwv.googlegroups. com>,
dated Tue, 7 Mar 2006 07:34:29 remote, seen in
news:comp.lang.javascript, adam.waterfield@gmail.com 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 <1141746461.320402.62500@u72g2000cwu.googlegroups. com>,
dated Tue, 7 Mar 2006 07:47:41 remote, seen in
news:comp.lang.javascript, Amie <amiekweon@gmail.com> 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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<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.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

 All times are GMT. The time now is 10:15 PM.