473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

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!

Adam

Mar 7 '06 #1
5 3886
Hi,

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

Hope it helps,
Amie

Mar 7 '06 #2
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
provide advice here.
PointedEars
Mar 7 '06 #3
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
provide advice here.
PointedEars
Mar 7 '06 #4
JRS: In article <11*********************@v46g2000cwv.googlegroups. com>,
dated Tue, 7 Mar 2006 07:34:29 remote, seen in
news:comp.lang.javascript, ad*************@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 <11*********************@u72g2000cwu.googlegroups. com>,
dated Tue, 7 Mar 2006 07:47:41 remote, seen in
news:comp.lang.javascript, Amie <am*******@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
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.

--
© 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.
Mar 7 '06 #5
ad*************@gmail.com 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:ad@ionicsoft.com
http://www.ionicsoft.com
Mar 8 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
by: patrick_woflian | last post by:
hey guys, im just writing a basic calculation at the moment, before building on it for an A-Level piece of work. i can add/divide etc... two numbers together yet i am having a major problem with...
22
by: Frinton | last post by:
Hi, I am trying to do some calculations on large numbers (ie 7,768,489,957,892,578,474,792,094 / 12,280) and no matter what I do it doesn't get it quite right. Its always somewhere between 10...
3
by: lawrencec | last post by:
Hi there, I'm trying to add the values of a number of form fields and to get a result at the end. It must loop and be able to dynamically update the result of calculation. I have attached the...
10
by: Ron | last post by:
I want to calculate the surface area of a sphere from an inputed radius with option strict on. I guess I am not converting something correctly. Here is what I am doing: I have a textbox...
2
by: shihaoran | last post by:
I really need help with one my program; it is about arraylist; I do not get it. Can someone please help me with it? Here's the instruction: 1. Your instructor will provide you with a text...
13
by: Ulv | last post by:
I have a table (TblItems) with fields Lenght, Width and Height as decimalfields. I also have the fields Desc, a text field. In a form I have created this line of code after updating: Desc =...
1
by: mcneilkm | last post by:
Hi all, I am a newby to this forum. I am making modifications to an existing database. The structure is basicall one primary record in a main table with many associated records in a second...
8
by: Abedin | last post by:
I have 9 digits in form of "111020402". Then I have another 9 digits in form of "111020403". I have 100,000 records of these two 9-digit numbers. I want to filter this as follows: District:...
3
by: Aussie Rules | last post by:
Hi, Whats the easiest way to round numbers up. For example, a calculation that results in a value of 1.3 will round to 1, and a calculation that results in 1.8 will round to 2. My issue...
10
by: Guillermo_Lopez | last post by:
Hello All, I am using VBA in access to perform some calculations. There is a particular sumation that is wrong (barely). this code is withing a loop. TDist = TDist + TempDist Both TDist...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.