![](/img/trans.png)
[英]How Close is the Javascript Math.Round to the C# Math.Round?
[英]How to correct for inaccurate Javascript math with math.round()?
我几天前就发布了这段代码,但是讨论似乎使人对Javascript的弱点(更不用说我自己作为“程序员”的明显弱点)有了哲理,然后在没有明确解决方案的情况下就消失了。 我希望有人能帮助纠正这一点。
由于明显地被称为“浮点数学”的现象,Javascript在下面的计算器代码中返回了算术上不准确的答案(正确答案下方为.00000004或类似内容)。 建议我通过“在变量上调用math.round()”来对答案进行四舍五入,我认为这可以很好地满足我的目的,只是我的JS功夫太弱了,因此在上下文中这样做的语法也是如此远远没有我。
我在哪里/如何拨打电话? 到目前为止,我所有的尝试都失败了,即使我确定每次尝试都不会失败。 考虑到我对这个主题的低级了解,我肯定会喜欢一个答案。 这一定是外面有人的灌篮。
<!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>
通常, 您需要四舍五入 ,因此可以一直保持数学准确性,直到显示数据为止。
这是我实现您的小图书馆的方法:
var SavingsLib = {
round : function(val, digits) {
var pow = Math.pow(10, digits);
return Math.round(pow * val) / pow;
},
padCurrency : function (val) {
var str = val.toString();
var parts = str.split(".");
return parts.length > 1 && parts[1].length == 1 ? str + "0" : str;
},
processForm : function() {
var daysPerMonth = parseInt(document.theForm4.elements[0].value);
var boardingsPerDay = parseInt(document.theForm4.elements[1].value);
var zone = document.getElementsByName("zonett")[0].value;
var savings = 0;
if(zone == "z4") savings = this.calculateSavings(daysPerMonth, boardingsPerDay, 4.25, 3.75);
else if(zone == "z3") savings = this.calculateSavings(daysPerMonth, boardingsPerDay, 3.75, 3.35);
else if(zone == "z2") savings = this.calculateSavings(daysPerMonth, boardingsPerDay, 3, 2.8);
else if(zone == "z1") savings = this.calculateSavings(daysPerMonth, boardingsPerDay, 2.5, 2.3);
else if(zone == "Base") savings = this.calculateSavings(daysPerMonth, boardingsPerDay, 1.5, 1.5);
this.showMessage(savings);
return false; // Don't submit form
},
calculateSavings : function(daysPerMonth, boardingsPerDay, price1, price2) {
var amount1 = (((daysPerMonth * boardingsPerDay) * price1) * 12);
var amount2 = (((daysPerMonth * boardingsPerDay) * price2) * 12);
return this.round(amount1 - amount2, 2);
},
showMessage : function(savings) {
alert("Your yearly savings if you buy Trip Tickets is $" + this.padCurrency(savings));
}
}
我的变更:
1.4500001
变为1.45
而不是1
) 1.4
变为1.40
) 这很简单 :
var prodZ4tt = Math.round(((one * two) * 4.25) *12) - (((one * two) * 3.75) *12);
尽管您可能会对语法感到困惑,这是可以理解的,但由于JavaScript以其直观的做事方式而闻名。 ;)
注意:这会将数字四舍五入到最接近的整数,而不是“四舍五入”。
您可以使用Math.round编写kludge,但这将使您不胜其烦(不确定在您的示例中是否适用,看起来好像可以)。 您可能想要的是固定的:
alert((102.000000004).toFixed(2)); // alerts "102.00"
这不是在IE中的四舍五入,但是根据我的判断,这对您来说并不重要。
(docs: https : //developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Number/toFixed,http : //www.codingforums.com/showthread.php ? t =102421 , http : //msdn.microsoft .com / zh-CN / library / sstyff0z.aspx )
编辑:打算将其放入评论中,但markdown在那里不起作用? 仍然习惯...
无论如何,如果您愿意,也可以在Math.round中使用kludge:
alert(Math.round(val * 100) / 100);
仍然可能显示愚蠢的浮动问题,您可以使用另一个.toFixed(现在您100%确信没有剩余的小数位不能正确舍入):
alert((Math.round(val * 100) / 100).toFixed(2));
编辑:集成:
将其添加到脚本块的顶部:
function roundNicely(val) {
return (Math.round(val * 100) / 100).toFixed(2);
}
然后在您的警报中,将值prodBasett
包装在函数调用中: roundNicely(prodBasett)
(其他警报也类似)
那有意义吗?
对于这种情况,当您需要精度时,您需要一个数学“任意精度库” ...您可以在这里找到一篇不错的文章。
http://blog.thatscaptaintoyou.com/introducing-big-js-arbitrary-precision-math-for-javascript/
Javascript本身具有round,ceil和floor来将浮点数转换为整数。
var fl = 349.12783691847;
Math.round(fl) // 349
Math.ceil(f1) // 350
Math.floor(fl) // 349
当您进行数学运算时,优良作法是使用分布性,并正确使用括号。
Math.round(((((a + b) * (c - d)) % e) * f));
请注意“ NaN”(http://en.wikipedia.org/wiki/NaN)和非Number类型。
祝你今天愉快
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.