Solved

String to Number

Posted on 2002-04-25
12
275 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
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
 

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now