简体   繁体   English

尝试在JavaScript中计算简单的折扣和营业税

[英]Trying to calculate a simple discount and sales tax in javascript

I'm just trying to input an number and then add 10% and then take that total and .08% tax. 我只是想输入一个数字,然后加10%,然后取总计和0.08%的税。 Can you please help me figure out what I'm doing wrong. 您能帮我弄清楚我在做什么错。 I sure it is something simple I'm overlooking but I'm new to this and can't figure what is wrong. 我确定这是我所忽略的简单事情,但是我对此并不陌生,无法弄清楚出什么问题了。

 <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Price tool</title> </head> <body> <form name="operation_form"> <label style="font-size:20px"><b>Price Tool</b></label> <br/> <br/> <br/> <label>Item Price: </label><input type="number" name='acertscore' value="" /> <br/> <br/> <label><b>Total is:</b></label><input type="number" name="answerbox"> <br/> <input type="button" value="Calculate" onclick="costCalc();" /> <br/> <input type="button" value="Reset" onClick="this.form.reset()" /> </form> <script type="text/javascript"> function costCalc() { var form = document.forms["operation_form"]; var x = form["acertscore"].value; var cost = (x * .1) + x; var answer = (cost * .08) + cost; form["answerbox"].value = answer; } </script> </body> </html> 

I logged the value of 'cost' in your code when I start with the value 100 当我从值100开始时,我在您的代码中记录了“费用”的值

  var cost = (x * .1) + x;
  console.log(cost);

and got a value of '10100' 的值为“ 10100”

But if I make sure that x is a number I get the correct value (110) 但是,如果我确定x是一个数字,我会得到正确的值(110)

  var cost = (x * .1) +  Number(x);
  console.log(cost);

And I get 118.8 for the total. 我总共得到118.8。

When you retrieve the value from your input control, it comes through as a string value. 从输入控件中检索值时,它作为字符串值出现。

var x = form["acertscore"].value; // x is a string

If you convert this to a number immediately you'll avoid additional problems. 如果立即将其转换为数字,将避免其他问题。

var x = Number(form["acertscore"].value); // x is a number

 function costCalc() { var form = document.forms["operation_form"]; var x = Number(form["acertscore"].value); var cost = (x * .1) + x; var answer = (cost * .08) + cost; form["answerbox"].value = answer; } 
 <form name="operation_form"> <label style="font-size:20px"><b>Price Tool</b></label> <br/> <br/> <br/> <label>Item Price: </label><input type="number" name="acertscore" value="" /> <br/> <br/> <label><b>Total is:</b></label><input type="number" name="answerbox"> <br/> <input type="button" value="Calculate" onclick="costCalc();" /> <br/> <input type="button" value="Reset" onClick="this.form.reset()" /> </form> 

The problem is that your answer number is so long that it tries to display exponents. 问题在于您的answer号太长,以至于无法显示指数。 Because of this, the number gets treated as a string, though you've specified that the output goes into an <input> field with a type of number . 因此,尽管已将输出指定为number type<input>字段,但该数字仍被视为字符串。 Because your answer is not a number, it can't be displayed. 由于您的answer不是数字,因此无法显示。

To resolve this, you can parse the output as a float before displaying it: 要解决此问题,您可以在显示输出之前将其解析为float

form["answerbox"].value = parseFloat(answer);

 function costCalc() { var form = document.forms["operation_form"]; var x = form["acertscore"].value; var cost = (x * .1) + x; var answer = (cost * .08) + cost; form["answerbox"].value = parseFloat(answer); } 
 <form name="operation_form"> <label style="font-size:20px"><b>Price Tool</b></label> <br/> <br/> <br/> <label>Item Price: </label><input type="number" name='acertscore' value="" /> <br/> <br/> <label><b>Total is:</b></label><input type="number" name="answerbox"> <br/> <input type="button" value="Calculate" onclick="costCalc();" /> <br/> <input type="button" value="Reset" onClick="this.form.reset()" /> </form> 

Hope this helps! 希望这可以帮助!

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

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