我正在使用javascript绑定到一些复选框,而toFixed(2)不是四舍五入.任何想法为什么它不四舍五入?例如,如果数字是859.385仅显示859.38而不是859.39.
我还读过,toFixed根据您使用的浏览器,可以进行不同的循环,任何人都知道如何解决这个问题,以便我的javascript计算与我的php计算相匹配?
var standardprice = parseFloat($('#hsprice_'+this.id.split('_')[1]).val());
var price = parseFloat($('#hprice_'+this.id.split('_')[1]).val());
var discount = parseFloat($('#hdiscount_'+this.id.split('_')[1]).val());
var deposit = parseFloat($('#hdeposit_'+this.id.split('_')[1]).val());
var currSprice = parseFloat($('#hTotalSprice').val());
var currPrice = parseFloat($('#hTotalPrice').val());
var currDiscount = parseFloat($('#hTotalDiscount').val());
var currDeposit = parseFloat($('#hTotalDeposit').val());
currSprice += standardprice;
currPrice += price;
currDiscount += discount;
currDeposit += deposit;
$('#lblTotalSprice').text('$'+addCommas(currSprice.toFixed(2)));
$('#lblTotalPrice').text('$'+addCommas(currPrice.toFixed(2)));
$('#lblTotalDiscount').text('$'+addCommas(currDiscount.toFixed(2)));
$('#lblTotalDeposit').text('$'+addCommas(currDeposit.toFixed(2)));
$('#hTotalSprice').val(currSprice.toFixed(2));
$('#hTotalPrice').val(currPrice.toFixed(2));
$('#hTotalDiscount').val(currDiscount.toFixed(2));
$('#hTotalDeposit').val(currDeposit.toFixed(2));
Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
for (var i=0;i<3;i++){
var num = i + 0.50;
var output = num + " " + Math.round(num) + " " + num.toFixed(0);
alert(output);
}
Run Code Online (Sandbox Code Playgroud)
在Opera 9.63中我得到:
0.5 1 0
1.5 2 2
2.5 3 2
在FF 3.03我得到:
0.5 1 1
1.5 2 2
2.5 3 3
在IE 7中我得到:
0.5 1 0
1.5 2 2
2.5 3 3
注意粗体结果.为什么会出现这种不一致的情况?这是否意味着toFixed(0)应该避免?将数字舍入到最接近的整数的正确方法是什么?
我正在尝试这样做,但我发现的只是四舍五入到最接近的整数。我想知道是否有办法用 math.round 来做到这一点,或者是否有不同的解决方案。谢谢!
可能重复:
你如何在Javascript中舍入到1位小数?
以下代码显示在谷歌地图上显示的特定路线上的总覆盖距离.我设法将数字从公里转换为英里.这是函数的代码:
function computeTotalDistance(result) {
var total = 0;
var myroute = result.routes[0];
for (i = 0; i < myroute.legs.length; i++) {
total += myroute.legs[i].distance.value;
}
total = total *0.621371/ 1000.
document.getElementById('total').innerHTML = total + ' mi';
Run Code Online (Sandbox Code Playgroud)
总计显示为41.76483039399999 mi.如何将total小数点四舍五入?
我在这里有一个小提琴:http://jsfiddle.net/94wQJ/1/ - 但也许有人可以通过下面看看建议.
<button type="button" id="allocate">Calc</button>
$('#allocate').click(function () {
val1 = 25.00;
val2 = 16.37;
val3 = val1-val2;
alert(val3);
});
Run Code Online (Sandbox Code Playgroud)
25 - 16.37 = 8.63 - 但是,val3的警报= 8.62999999999
为什么不准确?
谢谢,
标记
如何在javascript中使用ROUND HALF UP进行舍入?我使用的是Prototype JavaScript框架版本1.5.1_rc3,所以我更喜欢使用它.如果没有,我也很感激,如果你分享它.
任何指导表示赞赏.
如何在javascript.For中将十进制值更改为一位数字。例如。我有 5 值,我需要在我的 javascript 函数中显示 5.0。
下面写了一个从温度转换的函数
function tryConvert(temperature, convert /*callback*/) {
const input = parseFloat(temperature);
if (Number.isNaN(input)) {
return '';
}
const output = convert(input);
const rounded = Math.round(output * 1000) / 1000;
return rounded.toString();
}
Run Code Online (Sandbox Code Playgroud)
我的问题是这一行:
const rounded = Math.round(output * 1000) / 1000;
Run Code Online (Sandbox Code Playgroud)
为什么需要乘以1000?然后将结果除以 1000?