Solved

String to Number

Posted on 2002-04-25
12
282 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 50 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…
Suggested Courses

635 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