Link to home
Start Free TrialLog in
Avatar of RICHARDH
RICHARDH

asked on

String to Number

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
Avatar of fritz_the_blank
fritz_the_blank
Flag of United States of America image

You need to change the type by using the parseFloat() method,

i.e. a= parseFloat(form1.field1.value, 10) +
ASKER CERTIFIED SOLUTION
Avatar of fritz_the_blank
fritz_the_blank
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RICHARDH
RICHARDH

ASKER

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.
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.
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.
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
another way:

var a = (form1.field1.value*1) + (form1.field2.value*1);
I think
eval(form1.field1.value)
would work too.

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.


Richard,

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

Fritz the Blank
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
Having a tidy up. Thanks for the help on this one.