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

时间:2016-03-24 17:42:09

标签: javascript string numbers calculator parseint

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

我正在设计一个覆盖计算器来放在我公司的网站上。我的代码似乎接受输入,但返回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>

2 个答案:

答案 0 :(得分:0)

您的输入值应该通过parseInt()函数以10作为基数来进行,您应该检查是否没有使用isNaN(num)获得NaN。 Javascript是松散类型的,但这并不一定意味着你可以通过类型抛出变量而不检查它们。

答案 1 :(得分:0)

所以我修好了!

您的问题原因是您从输入中获取数据的方式。

你用过这个:

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>