[英]IEEE 754 floating point arithmetic rounding error in c# and javascript
[英]Does this === not work b/c of floating point rounding inaccuracies?
假设我有这个片段。
var age = prompt('what is your age?');
if (age === 30)
{
alert('your age is 30');
}
当我在提示中输入30时,if语句不会触发。 我怀疑这与浮点舍入错误有关,但并不确定。 很高兴听到您对此的想法。
window.prompt()
返回一个字符串。 字符串与数字不同。 使用以下任一方法:
if (age === '30')
if (+age === 30) // Explicit type conversion
if (age == 30) // Implicit type conversion
有关显式转换的说明,请参见此答案 。 例如,输入00030
也可能有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.