好吧,我远非数学专家。哎呀,我记得足够的高中代数拼凑任何工作公式这一事实对我来说是一个胜利。因此,如果你发现这里的公式过长或令人困惑,那就解释了这一点。
但是,正如人们可以合理地预期的那样,这里出了点问题。
我正在尝试制作一个简单的计算器,显示购买旅行机票和支付现金票价所节省的成本。我非常接近,但是我使用的公式的计算在某些情况下并不正常,我无法弄清楚原因。
在下面的示例中,Base区域和区域4按我的预期计算差异,而区域1-3返回的答案仅比正确答案低十几位(即$ 105.60000000000014而不是$ 106),而区域1和2返回虽然公式不同,但答案相同。
我看着这个,直到我走了。我确定答案很简单,但我看不清楚。任何人
感谢您的帮助。
以下是代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script language="javascript">
<!-- Begin Trip Tickets Savings Calc script
function doMath4() {
var one = parseInt(document.theForm4.elements[0].value);
var two = parseInt(document.theForm4.elements[1].value);
var selection = document.getElementsByName("zonett")[0].value;
if(selection == "z4"){
var prodZ4tt = (((one * two) * 4.25) *12) - (((one * two) * 3.75) *12);
alert("Your yearly savings if you buy Trip Tickets is $" + prodZ4tt + ".");
}
else if(selection == "z3"){
var prodZ3tt = (((one * two) * 3.75) *12) - (((one * two) * 3.35) *12);
alert("Your yearly savings if you buy Trip Tickets is $" + prodZ3tt + ".");
}
else if(selection == "z2"){
var prodZ2tt = (((one * two) * 3) *12) - (((one * two) * 2.8) *12);
alert("Your yearly savings if you buy Trip Tickets is $" + prodZ2tt + ".");
}
else if(selection == "z1"){
var prodZ1tt = (((one * two) * 2.5) *12) - (((one * two) * 2.3) *12);
alert("Your yearly savings if you buy Trip Tickets is $" + prodZ1tt + ".");
}
else if(selection == "Base"){
var prodBasett = (((one * two) * 1.5) *12) - (((one * two) * 1.5) *12);
alert("Your yearly savings if you buy Trip Tickets is $" + prodBasett + ".");
}
}
// End Trip Tickets Savings Calc script -->
</script>
</head>
<body>
<form name="theForm4" class="calcform">
<h2>You Do the Math: Commuter Express Trip Tickets Vs. Cash</h2>
<div class="calcform-content">
<div class="formrow-calc">
<div class="calcform-col1">
<p>Days you commute on Commuter Express monthly:</p>
</div>
<div class="calcform-col2">
<input type="text">
</div>
<div class="calcform-col3"> </div>
</div>
<div class="clear"></div>
<div class="formrow-calc">
<div class="calcform-col1">
<p>Daily boardings on Commuter Express Bus:</p>
<table border="0" cellspacing="0" cellpadding="0" class="fareexampletable">
<tr>
<td colspan="2" class="savingsleft"><p class="ifyouride">EXAMPLE:</p></td>
</tr>
<tr>
<td class="savingsleft"><p><strong>Go to work:</strong></p></td>
<td class="savingsright"><p>1 time</p></td>
</tr>
<tr>
<td class="savingsleft"><p><strong>Come home from work:</strong></p></td>
<td class="additionline savingsright"><p>+1 time</p></td>
</tr>
<tr>
<td class="savingsleft"><p><strong>Total:</strong></p></td>
<td class="savingsright"><p>2 times</p></td>
</tr>
</table>
</div>
<div class="calcform-col2">
<input type="text">
</div>
<div class="calcform-col3"> </div>
</div>
<div class="clear"></div>
<div class="formrow-calc savings-zone">
<div class="calcform-col1">
<p>Choose Zone:</p>
</div>
<div class="calcform-col2">
<select name="zonett">
<option value="Base">Base</option>
<option value="z1">Zone 1</option>
<option value="z2">Zone 2</option>
<option value="z3">Zone 3</option>
<option value="z4">Zone 4</option>
</select>
</div>
</div>
<div class="formrow-calc">
<div class="calcform-col4-ce">
<button type="submit" onclick="doMath4()" class="btn-submit"><div class="btn-submit"><img src="img/btn_savings.png" alt="Show My Yearly Savings" /></div></button>
</div>
</div>
<div class="clear">
</div>
</div>
</form>
</body>
</html>
答案 0 :(得分:0)
以下是我在评论中讨论的清理代码:
function doMath4() {
var one = parseInt(document.theForm4.elements[0].value);
var two = parseInt(document.theForm4.elements[1].value);
var selection = document.getElementsByName("zonett")[0].value;
var m12 = one * two * 12;
var prod = null;
if(selection == "z4")
{
prod = (m12 * 4.25) - (m12 * 3.75);
}
else if(selection == "z3")
{
prod = (m12 * 3.75) - (m12 * 3.35);
}
else if(selection == "z2")
{
prod = (m12 * 3) - (m12 * 2.8);
}
else if(selection == "z1")
{
prod = (m12 * 2.5) - (m12 * 2.32);
}
else if(selection == "Base")
{
prod = (m12 * 1.5) - (m12 * 1.5); //This is always 0?
}
if(prod != null)
{
prod = Math.round(prod);
alert("Your yearly savings if you buy Trip Tickets is $" + prod + ".");
}
}
上述(及以下)包含了您问题的最简单解决方案:
prod = Math.round(prod);
这会将结果(prod)四舍五入到最接近的整数,无论它是什么。因此,在您的示例中,$ 105.600000 ...将被舍入到$ 106,这就是您要求的。