计算返回带有动态值的 NaN
如果我手动对值进行编码,则以下计算工作正常,但对于动态值,我总是以 NaN 结束。
vals.tension = Math.round( ( Math.pow( ( vals.frequency * ( vals.stringLength * 10 ) * vals.diameter ), 2 ) * ( Math.PI * vals.density / 9810000000000 ) ) * 100 ) / 100;
如果我添加以下值,一切都可以正常工作:
Math.round( ( Math.pow( ( 389.723 * ( 66 * 10 ) * .44 ), 2 ) * ( Math.PI * 1300 / 9810000000000 ) ) * 100 ) / 100;
// 5.33
如果我手动写出这些值,它也可以正常工作:
var vals = {
frequency: 389.723,
stringLength: 66,
diameter: .44,
density: 1300
};
但如果我尝试通过用户输入动态地重新创建完全相同的值,我会得到 NaN。我已经通过 console.log 检查了这些值,它们完全相同(它们不是字符串),并且它们都已正确添加到对象中,但动态执行时的实际计算(在 Safari 中)返回 NaN。
The following calculation works fine if I code the values by hand, but with dynamic values I always end up with NaN.
vals.tension = Math.round( ( Math.pow( ( vals.frequency * ( vals.stringLength * 10 ) * vals.diameter ), 2 ) * ( Math.PI * vals.density / 9810000000000 ) ) * 100 ) / 100;
If I add the following values it all works fine:
Math.round( ( Math.pow( ( 389.723 * ( 66 * 10 ) * .44 ), 2 ) * ( Math.PI * 1300 / 9810000000000 ) ) * 100 ) / 100;
// 5.33
If I write out the values by hand it also works ok:
var vals = {
frequency: 389.723,
stringLength: 66,
diameter: .44,
density: 1300
};
But if I try to dynamically re-create exactly the same values via user input I get NaN. I've checked the values via console.log and they are EXACTLY the same (they're not strings) and they all get added ok to the object, but the actual calculation when preformed dynamically (in Safari) returns NaN.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将用户输入包装到 parseFloat() 函数中
Try to wrap your user input into parseFloat() function
尝试使用 parseFloat() 或 parseInt() 函数。 parseFloat 的小数部分和浮点部分之间的分隔符应该是点。
并且在计算之前可以在条件中使用Isnumeric()来确保它是正确的输入
try to use parseFloat() or parseInt() functions. The delimiter between decimal and float part for parseFloat should be dot.
And you may use Isnumeric() in condition before calculation to make sure that it is right input