admin管理员组文章数量:1289788
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 parseFloat
and parseInt
but 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.
I'm new here and I need help, please don't troll :) I am trying to add a cost calculator to my website.
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 parseFloat
and parseInt
but 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.
I'm new here and I need help, please don't troll :) I am trying to add a cost calculator to my website.
Share Improve this question edited Dec 19, 2012 at 8:01 Blachshma 17.4k4 gold badges61 silver badges73 bronze badges asked Dec 19, 2012 at 3:54 JayJay 6666 gold badges16 silver badges31 bronze badges 6-
1
typeof NaN
is also"number"
, could youalert(dm)
instead of the last? – Bergi Commented Dec 19, 2012 at 3:58 - 2 Works for me: jsfiddle/HfCrD – Tim M. Commented Dec 19, 2012 at 4:00
-
6
What is the real value of
DriverD
element – Shiplu Mokaddim Commented Dec 19, 2012 at 4:01 - DriverD is a textbox and I am using 10 during troubleshooting of this issue. My website is a simple html/javascript site. – Jay Commented Dec 19, 2012 at 4:09
- @user1914656 Can you re-create the problem on JSFiddle or link to a live version we can see? – Phil Commented Dec 19, 2012 at 4:24
5 Answers
Reset to default 1It seems fine to me.
I want to tell another possible problem: You dont check if document.getElementById("DriverD").value
is a number or not. If a user enters a string or other type, it will cause a problem.
Try this
Two operands must be convert to 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
The only reason you could get NaN
here is that the value of dd
is NaN
. Because the value of dm
is defined as dd - dml
and the value of dml
is a Number
value, 30
.
parseFloat()
always returns a Number
value. The -
operator converts its operands to Number
only if necessary. So the value of dm
must by definition be a Number
value.
However, NaN
is a Number
value (typeof NaN === "number"
), and it is "toxic": once an operand is NaN
, the result of all following arithmetic operations is 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.
One mon reason for that is that the user has typed thousands separators (which are not supported) or a decimal ma while only a decimal point is supported.
Try this 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
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.
本文标签: javascriptGetting NaN when adding variables which consists of another variablesStack Overflow
版权声明:本文标题:javascript - Getting NaN when adding variables which consists of another variables - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741416786a2377559.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论