javascript - 将两个变量相除返回 NaN

标签 javascript nan

当我将两个变量相除得到变量 v1 时,它返回 NaN?但是,当我使用 document.write 时,所有其他变量都会给出数字

    min = 0.001,max = 0.1,
    c1 = (Math.random() * (max - min) + min).toFixed(3);

    min = 0.1,max = 1000,
    kcat = (Math.random() * (max - min) + min);

    var Eo = 0.1;
    var Vmax = (Eo*kcat);

    min = 0.1,max = 100,
    Km = (Math.random() * (max - min) + min);

    v1t = (c1)*(Vmax);
    document.write(v1t); //GIVES NUMBER
    v1b = (c1)+(Km);
    document.write(v1b); //GIVES NUMBER

v1 = v1t / v1b;
document.write(v1); //returns NaN

最佳答案

toFixed 构建一个字符串(例如 "0.013")。当您使用 (c1)+(Km) 添加一个数字时,它会给出如下字符串:

"0.01339.069145169761036"

使用 v1t/v1b 将数字除以字符串得到 NaN

为什么要使用 toFixed?似乎没有任何充分的理由这样做。但是,如果您真的想使用它,请在将其用于计算之前将 parseFloat 应用于结果。

关于javascript - 将两个变量相除返回 NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17784772/

相关文章:

asp.net - 如何将变量添加到服务器端绑定(bind)

javascript - Uncaught ReferenceError : (function) is not defined

javascript - 使用 ajax 和 for 循环将多个 JSON 对象一一推送/添加到 API

python - 在python中将 "nan"移到数组的开头

javascript - 如何使用属性值为 NaN 的 should.js 检查对象是否相等

python - pandas concat 生成 nan 值

javascript - Express Server 绕过 CORS

javascript - postman 测试抛出 "TypeError: Cannot read property ' get' of undefined"

python - 相干图空白 - nan 的相干值

java - Double.isNaN() 是如何工作的?