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 badges3 Answers
Reset to default 3Explicitly. 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().
本文标签:
版权声明:本文标题:In a JavaScript, should I call parseFloat to convert strings to floating point numbers, or should I let JavaScript convert them? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745440546a2658428.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论