javascript 添加由另一个变量组成的变量时获取 NaN

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13945257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 20:15:51  来源:igfitidea点击:

Getting NaN when adding variables which consists of another variables

javascriptnan

提问by Jay

var dml = 30
var dd = parseFloat(document.getElementById("DriverD").value)     <----- Only numbers like 10
var dm = dd-dml

alert((dd - dml) * 0.75)    <----- This works
alert(dm * 0.75)            <----- This returns NaN
alert(typeof(dm))        <----- This show that dm is a Number

I'm not sure why I keep getting NaN. I already tried parseFloatand parseIntbut still showing NaN when multiplying a variable (dm) which consists of variables (dd-dml). dm is the result of subtracting dml with dd or 10-30. Please share your solutions.

我不确定为什么我一直收到 NaN。我已经尝试过parseFloatparseInt但在乘以由变量 (dd-dml) 组成的变量 (dm) 时仍然显示 NaN。dm 是用 dd 或 10-30 减去 dml 的结果。请分享您的解决方案。

I'm new here and I need help, please don't troll :) I am trying to add a cost calculator to my website.

我是新来的,我需要帮助,请不要巨魔:) 我正在尝试向我的网站添加成本计算器。

回答by smttsp

It seems fine to me.

对我来说似乎很好。

I want to tell another possible problem: You dont check if document.getElementById("DriverD").valueis a number or not. If a user enters a string or other type, it will cause a problem.

我想告诉另一个可能的问题:你不检查是否document.getElementById("DriverD").value是一个数字。如果用户输入字符串或其他类型,则会导致问题。

回答by PointedEars

The only reason you could get NaNhere is that the value of ddis NaN. Because the value of dmis defined as dd - dmland the value of dmlis a Numbervalue, 30.

你能得到的唯一原因,NaN这里要说的是价值ddNaN。因为 的值dm被定义为dd - dml并且 的值dml是一个Number值,30

parseFloat()always returns a Numbervalue. The -operator converts its operands to Numberonly if necessary. So the value of dmmust by definition be a Numbervalue.

parseFloat()总是返回一个Number值。该-操作其操作数转换为Number在必要时只。因此,dm根据定义,值必须是一个Number值。

However, NaNis a Numbervalue (typeof NaN === "number"), and it is "toxic": once an operand is NaN, the result of all following arithmetic operations is NaN.

然而,NaN是一个Number值 ( typeof NaN === "number"),它是“有毒的”:一旦一个操作数是NaN,其后所有算术运算的结果就是NaN

That means that parseFloat()must have returned NaN, which means that the argument to parseFloat()could not be interpreted as the prefix of a decimal number representation.

这意味着parseFloat()必须返回NaN,这意味着parseFloat()无法将参数解释为十进制数字表示的前缀。

One common reason for that is that the user has typed thousands separators (which are not supported) or a decimal comma while only a decimal point is supported.

一个常见的原因是用户输入了千位分隔符(不支持)或十进制逗号,而只支持小数点。

回答by iftkhar

Trythis one

试试这个

    var dml = 30;
    var dd = parseFloat(document.getElementById("DriverD").value)   ;  <----- Only numbers like 10
    var dm=0;
    dm = dd-dml;

    alert((dd - dml) * 0.75);    <----- This works
    alert(dm * 0.75);            <----- This returns NaN
    alert(typeof(dm));        <----- This show that dm is a Number

回答by Sebastian Korszon

I am a newbie but I think it is the type error, solution:

我是新手,但我认为是类型错误,解决方法:

var someV  = parseInt(document.getElementById("some-v-id").value, 10) || 0,  

parseInt(value, 10) - you know because you used parse float, but two pipes and zero might prevent you from NaN as JavaSrcipt might be treating 0 as a string.

parseInt(value, 10) - 您知道,因为您使用了解析浮点数,但是两个管道和零可能会阻止您使用 NaN,因为 JavaSrcipt 可能会将 0 视为字符串。

回答by Asish AP

Try this

试试这个

Two operands must be convert to parseFloat

两个操作数必须转换为 parseFloat

<input type="text:" value="10" id="DriverD" />

var dml = parseFloat(30);
var dd = parseFloat(document.getElementById("DriverD").value);
var dm = dd-dml;
alert(dm * .10);

Result is -.2

结果是 -.2