admin管理员组

文章数量:1425168

I'm writing some JavaScript that accepts numerical inputs from text fields, and runs them through some equations. These equations use multiplication, subtraction, addition, division, and exponents. All testing so far seems to indicate that JavaScript is converting the numerical text inputs (type string) to type number and calculating the equation results correctly. Should I continue to trust JavaScript to implicitly convert the input values from type string to type number, or should I explicitly convert each input value using parseFloat()?

Thanks in advance for any advice or insight provided!

EDIT: To clarify, I am taking an input, checking to make sure that it is an integer. If the input value is not an integer, an alert is displayed and no calculations are done. If the input value is an integer, then it is run through an equation in which the input is immediately multiplied (0.01*value), or has an exponent applied (Math.pow(value, 2)). This is done twice for two separate inputs. Another equation takes the results of the first two equations and immediately divides them (0.5/value). Given these operations, is implicit conversion safe?

I'm writing some JavaScript that accepts numerical inputs from text fields, and runs them through some equations. These equations use multiplication, subtraction, addition, division, and exponents. All testing so far seems to indicate that JavaScript is converting the numerical text inputs (type string) to type number and calculating the equation results correctly. Should I continue to trust JavaScript to implicitly convert the input values from type string to type number, or should I explicitly convert each input value using parseFloat()?

Thanks in advance for any advice or insight provided!

EDIT: To clarify, I am taking an input, checking to make sure that it is an integer. If the input value is not an integer, an alert is displayed and no calculations are done. If the input value is an integer, then it is run through an equation in which the input is immediately multiplied (0.01*value), or has an exponent applied (Math.pow(value, 2)). This is done twice for two separate inputs. Another equation takes the results of the first two equations and immediately divides them (0.5/value). Given these operations, is implicit conversion safe?

Share Improve this question edited May 24, 2012 at 19:31 mskfisher 3,4124 gold badges37 silver badges49 bronze badges asked Apr 23, 2011 at 10:24 RyanRyan 6597 silver badges13 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Explicitly. Not sure what you are seeing but it doesn't really work

var a = "1";
var b = 2;

a + b  = "12"
b + a = "21"

Implicitly converting them would have the same effect as using parseFloat except when the string contains characters, where parseFloat ignores characters after the float.ie.

(+"12.5") == parseFloat("12.5"); // true

and

isNaN(+"a12.5") == true;
isNaN(parseFloat("a12.5")) == true;

however

(+"12.5s"); // is NaN but
parseFloat("12.5s") == 12.5;

So it would depend on what types of inputs you're accepting.

If you don't use parseFloat() explicitly, your strings could remain strings past the point of input and accidently be used as strings at some point later in your code resulting in a concatenation or other accidental string operation.

Also, if your input in badly formed, parseFloat() will return NaN which you can test for.

If you are expecting a float, use parseFloat().

本文标签: