繁体   English   中英

在计算器中将字符串转换为数字

[英]Convert string to number in calculator

我不是行业程序员,所以请不要对我提出简单的问题。 我相信答案很简单,但我无法弄清楚。

我正在尝试设计一个覆盖计算器,以将其放到我公司的网站上。 我的代码似乎接受了输入,但是返回了NaN值。 显然,正如我所料,它正在将信息转换为字符串。 我尝试通过从我的最后一个变量减去0并使用parseInt将其转换回数字。 似乎都不起作用。 有人愿意解释如何解决此问题以及您的解决方案为何起作用。 我试图了解我做错了什么。

请参见下面的代码。

谢谢。

<!DOCTYPE html>
<html>

<head>

</head>

<body>
<h1>Mulch Calculator</h1>

<form name="calcInput" id="calcInfoInput">

The length of the area you need to cover (in feet). <input type="text" name="length"
<br></br>
<br></br>
The width of the area you need to cover (in feet). <input type="text" `enter code here`name="width"
<br></br>
<br></br>
The depth of coverage you need (in inches). <input type="text" name="depth"
<br></br>
<br></br>
</form>
<input type="submit" value="Calculate Your Mulch Needs." onclick="processForm()" name="submit">
<input type="reset" value="Clear This Form.">

</form>
<script type ="text/javascript">


function processForm()
{
var allInfo = document.getElementById ("calcInfoInput");
var bedLength = allInfo.elements["length"].value;
var bedWidth = allInfo.elements["width"].value;
var bedDepthInches = allInfo.elements["depth"].value;
var squareFeet = bedLength * bedWidth;
var bedDepth = bedDepthInches / 12;
var cubicFeet = squareFeet * bedDepth;
var cubicYards = cubicFeet / 27;
//cubicYards = cubicYards-0;
//cubicYards = parseInt( cubicYards, 10 );
document.write('You will need at least '+ cubicYards +' cubic yards for your project.');
}

</script>

</body>

输入值应通过parseInt()函数(基数为10)输入,并且应检查isNaN(num)是否得到NaN。 Javascript是松散类型的,但这并不一定意味着您可以在不检查类型的情况下通过类型抛出变量。

所以我已经解决了!

您的问题原来是如何从输入中获取数据。

您使用了这个:

var bedLength = allInfo.elements["length"].value;

我用这个:

var bedLength = +allInfo["length"].value;

因此,基本上,我删除了使用输入值的每个位置的.elements

我还补充说:

calcscale = (calcscale).toFixed(3);

将小数位修整为3(因此您不会得到3.111111111111111117之类的答案)!

 function calculate() { var allInfo = document.getElementById("calcInfoInput"); var bedLength = +allInfo["length"].value; var bedWidth = +allInfo["width"].value; var bedDepthInches = +allInfo["depth"].value; var squareFeet = bedLength * bedWidth; var bedDepth = bedDepthInches / 12; var cubicFeet = squareFeet * bedDepth; var cubicYards = cubicFeet / 27; //cubicYards = cubicYards-0; //cubicYards = parseInt( cubicYards, 10 ); output = (cubicYards).toFixed(3); document.getElementById("result").innerHTML = 'You will need at least ' + output + ' cubic yards for your project.'; } 
 <body> <h1>Mulch Calculator</h1> <form name="calcInput" id="calcInfoInput"> The length of the area you need to cover (in feet). <input type="text" name="length" /> <br> <br>The width of the area you need to cover (in feet). <input type="text" name="width" /> <br> <br>The depth of coverage you need (in inches). <input type="text" name="depth" /> <br> <br> </form> <input type="submit" id="byBtn" value="Calculate You're Mulch Needs." onclick="calculate()" /> <input type="reset" value="Clear This Form."> <br> <br> <div id="result"></div> </form> </body> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM