简体   繁体   English

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

[英]Convert string to number in calculator

I am not a programmer by trade so please don't hold my simple question against me. 我不是行业程序员,所以请不要对我提出简单的问题。 I am sure the answer is simple, but I can't figure it out. 我相信答案很简单,但我无法弄清楚。

I am trying to design a mulch calculator to put on my company's website. 我正在尝试设计一个覆盖计算器,以将其放到我公司的网站上。 My code appears to accept the input, but is returning a NaN value. 我的代码似乎接受了输入,但是返回了NaN值。 It is obviously converting to info to a string as I expected. 显然,正如我所料,它正在将信息转换为字符串。 I have tried converting it back to a number by subtracting a 0 from my last variable and also using parseInt. 我尝试通过从我的最后一个变量减去0并使用parseInt将其转换回数字。 Neither seemed to work. 似乎都不起作用。 Would someone be willing to explain how to fix this issue, and why your solution worked. 有人愿意解释如何解决此问题以及您的解决方案为何起作用。 I am trying to understand what I did wrong. 我试图了解我做错了什么。

See the code below. 请参见下面的代码。

Thanks. 谢谢。

<!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>

Your input values should be put through the parseInt() function with 10 as your radix and you should check that you don't get NaN with the isNaN(num). 输入值应通过parseInt()函数(基数为10)输入,并且应检查isNaN(num)是否得到NaN。 Javascript is loosely typed but that doesn't necessarily mean you can throw variables around through types without checking them. Javascript是松散类型的,但这并不一定意味着您可以在不检查类型的情况下通过类型抛出变量。

So I have fixed it! 所以我已经解决了!

Your problem turned out to be how you were getting the data from the input. 您的问题原来是如何从输入中获取数据。

You used this: 您使用了这个:

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

I used this: 我用这个:

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

So essentially I removed the .elements for each place that you use the input values. 因此,基本上,我删除了使用输入值的每个位置的.elements

I also added: 我还补充说:

calcscale = (calcscale).toFixed(3);

Which trims the decimal place to 3 ( so you dont get an answer like 3.111111111111111117 or something like that)! 将小数位修整为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