Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

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
0
RICHARDH
Asked:
RICHARDH
1 Solution
 
fritz_the_blankCommented:
You need to change the type by using the parseFloat() method,

i.e. a= parseFloat(form1.field1.value, 10) +
0
 
fritz_the_blankCommented:
Sorry, a premature submit! Here is the whole thing

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

Fritz the Blank
0
 
RICHARDHAuthor Commented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RICHARDHAuthor Commented:
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
 
RICHARDHAuthor Commented:
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
 
fritz_the_blankCommented:
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
 
knightEknightCommented:
another way:

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

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
 
fritz_the_blankCommented:
Richard,

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

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now