javascript 收到 NaN 错误,不知道为什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15083724/
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 23:25:48  来源:igfitidea点击:

Getting a NaN error, can't figure out why

javascriptmath

提问by notknown7777

I have this code in JavaScript:

我在 JavaScript 中有这个代码:

SolarPanels = parseInt(lRemainingWidth / (panel_width + ( 2 * lPanelInterSpace)));

and then I alert the SolarPanelsvalue which gives NaNas output,

然后我提醒作为输出SolarPanels给出的值NaN

alert("SolarPanels "+SolarPanels); 

This 1 line is a tiny part of a huge calculation, but my code seems to fail here, with the use of alerts i've read out the values of SolarPanels, lRemainingWidth, panel_widthand lPanelInterSpacewhich are the following:

这1线是一个巨大的计算的一小部分,但我的代码似乎在这里失败,使用警报的我已经读出的值SolarPanelslRemainingWidthpanel_widthlPanelInterSpace它有以下几种:

lRemainingwidth = 17.4.320227272727276
SolarPanels = 0
panel_width = 1.65
lPanelInterSpace = 0.02

I think it has to do with the 2 dots in lRemainingWidth, either way, I don't know how to fix it. Why the lRemainingWidthhas 2 dots?

我认为这与 中的 2 个点有关lRemainingWidth,无论哪种方式,我都不知道如何解决。为什么lRemainingWidth有2个点?



Update :

更新 :

This is the part that calculates the lRemainingWidth :

这是计算 lRemainingWidth 的部分:

    if(HalforDouble === "Yes")
    {
        lRemainingWidth = (roof_ridge /2) + ((lRemainingHeight / Math.tan((lRoofEdgeDegrees * Math.PI) / 180)) - lRoofEdge);            
    }

    else
    {
        lRemainingWidth = roof_ridge + (2 * ((lRemainingHeight / Math.tan((lRoofEdgeDegrees * Math.PI) / 180)) - lRoofEdge));
    }

The values here are:

这里的值是:

lRemainingWidth = 0
roof_ridge = 17
lRemainingHeight = 20.769000000000002
lRoofEdgeDegrees = 83.5169263071276
lRoofEdge = 0.2

回答by Denys Séguret

Your problem is that you mix strings and numbers

你的问题是你混合了字符串和数字

Start with this code before any computation :

在任何计算之前从以下代码开始:

var roof_ridge = parseFloat(roof_ridge);

There might be other strings hidden in your code but we don't see them. Apply the same conversion on them.

您的代码中可能隐藏了其他字符串,但我们看不到它们。对它们应用相同的转换。

回答by Mike Samuel

lRemainingWidth = roof_ridge + (2 * ((lRemainingHeight / Math.tan((lRoofEdgeDegrees * Math.PI) / 180)) - lRoofEdge));
lRemainingWidth = roof_ridge + (2 * ((lRemainingHeight / Math.tan((lRoofEdgeDegrees * Math.PI) / 180)) - lRoofEdge));

If roof_ridgeis a string then the +does string concatenation instead of addition.

如果roof_ridge是字符串,则+执行字符串连接而不是加法。

Change this to

将此更改为

lRemainingWidth = +roof_ridge + (2 * ((lRemainingHeight / Math.tan((lRoofEdgeDegrees * Math.PI) / 180)) - lRoofEdge));

The prefix +operator in +roof_ridgecoerces its argument to a number.

前缀+运算符 in 将+roof_ridge其参数强制为数字。

回答by Sergii Stotskyi

It seems like a cornerstone of the issue in roof_ridgevariable. This variable is instance of String class, not a number. So, when you code go to this line:

这似乎是roof_ridge变量问题的基石。此变量是 String 类的实例,而不是数字。因此,当您编写代码时,请转到这一行:

lRemainingWidth = roof_ridge + (2 * ((lRemainingHeight / Math.tan((lRoofEdgeDegrees * Math.PI) / 180)) - lRoofEdge));

the next calculation is done:

下一个计算完成:

'17' + whatever_float_value = got string concatenation instead of number's sum.

To fix this just add:

要解决这个问题,只需添加:

lRemainingWidth = parseFloat(roof_ridge) + (2 * ((lRemainingHeight / Math.tan((lRoofEdgeDegrees * Math.PI) / 180)) - lRoofEdge));