admin管理员组文章数量:1200965
As you know, the javascript's parseFloat
function works only until it meets an invalid character, so for example
parseFloat("10.123") = 10.123
parseFloat("12=zzzz") = 12
parseFloat("z12") = NaN
Is there a way or an implementation of parseFloat that would return NaN if the whole string is not a valid float number?
As you know, the javascript's parseFloat
function works only until it meets an invalid character, so for example
parseFloat("10.123") = 10.123
parseFloat("12=zzzz") = 12
parseFloat("z12") = NaN
Is there a way or an implementation of parseFloat that would return NaN if the whole string is not a valid float number?
Share Improve this question asked Jul 15, 2010 at 15:28 mcm69mcm69 7306 silver badges14 bronze badges 03 Answers
Reset to default 22Use this instead:
var num = Number(value);
Then you can do:
if (isNaN(num)) {
// take proper action
}
Maybe try:
var f = parseFloat( someStr );
if( f.toString() != someStr ) {
// string has other stuff besides the number
}
Update: Don't do this, use @dcp's method :)
var asFloat = parseFloat("12aa");
if (String(asFloat).length != "12aa".length) {
// The value is not completely a float
}
else {
// The value is a float
}
本文标签: javascriptIs it possible to parseFloat the whole stringStack Overflow
版权声明:本文标题:javascript - Is it possible to parseFloat the whole string? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738617572a2102998.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论