Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

String to Number

Posted on 2002-04-25
12
Medium Priority
?
284 Views
Last Modified: 2008-03-10
I am writing a javascript function which takes information from fields in a form and then adds them up.

eg a=form1.field1.value+form1.field2.value+ ...

The trouble is the values are taken as strings which are then combined ie 2.1 + 3.2 will give 2.13.2

How can I force the values to be numbers.

Thanks for the help.

Richard.H
0
Comment
Question by:RICHARDH
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
12 Comments
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 6969020
You need to change the type by using the parseFloat() method,

i.e. a= parseFloat(form1.field1.value, 10) +
0
 
LVL 46

Accepted Solution

by:
fritz_the_blank earned 200 total points
ID: 6969026
Sorry, a premature submit! Here is the whole thing

a= parseFloat(form1.field1.value, 10) + parseFloat(form1.field2.value, 10)

Fritz the Blank
0
 

Author Comment

by:RICHARDH
ID: 6969035
Thanks.....

A question if I may...

What does the 10 represent as the second parameter in brackets.

parseFloat(form1.field1.value, 10)

Thanks once again.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:RICHARDH
ID: 6969041
Thanks.....

A question if I may...

What does the 10 represent as the second parameter in brackets.

parseFloat(form1.field1.value, 10)

Thanks once again.
0
 

Author Comment

by:RICHARDH
ID: 6969052
Thanks.....

A question if I may...

What does the 10 represent as the second parameter in brackets.

parseFloat(form1.field1.value, 10)

Thanks once again.
0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 6969069
The second parameter has to do with the base, for some odd reason the default is base 8! Adding the 10 makes sure that your number gets processed the way you expect it to.

Fritz the Blank
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 6969203
another way:

var a = (form1.field1.value*1) + (form1.field2.value*1);
0
 
LVL 19

Expert Comment

by:webwoman
ID: 6969425
I think
eval(form1.field1.value)
would work too.
0
 
LVL 30

Expert Comment

by:third
ID: 6970257

parseFloat Method
Returns a floating-point number converted from a string.

Syntax : parseFloat(numString)

The required numString argument is a string that contains a floating-point number.

Remarks
The parseFloat method returns a numerical value equal to the number contained in numString. If no prefix of numString can be successfully parsed into a floating-point number, NaN (not a number) is returned.

parseFloat("abc")      // Returns NaN.
parseFloat("1.2abc")   // Returns 1.2.
You can test for NaN using the isNaN method

---------------------------------------
parseInt Method
Returns an integer converted from a string.

Syntax : parseInt(numString, [radix])

Arguments
numString - Required. A string to convert into a number.

radix - Optional. A value between 2 and 36 indicating the base of the number contained in numString. If not supplied, strings with a prefix of '0x' are considered hexidecimal and strings with a prefix of '0' are considered octal. All other strings are considered decimal.

Remarks
The parseInt method returns an integer value equal to the number contained in numString. If no prefix of numString can be successfully parsed into an integer, NaN (not a number) is returned.

parseInt("abc")     // Returns NaN.
parseInt("12abc")   // Returns 12.


0
 
LVL 46

Expert Comment

by:fritz_the_blank
ID: 6971159
Richard,

You have a lot of information here. Did you need anything else? If not, please close out this question.

Fritz the Blank
0
 

Expert Comment

by:ananth222
ID: 6974771
The simplest way to do it is like this:
field1.value * 1 converts the string to a number in javascript!

so u have:
num1 = field1.value * 1;
num2 = field2.value * 1;
...
total = num1 + num2 + ...;

try it out!

~A
0
 

Author Comment

by:RICHARDH
ID: 7118130
Having a tidy up. Thanks for the help on this one.
0

Featured Post

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question