相关疑难解决方法(0)

在JavaScript中格式化一个正好两位小数的数字

我有这行代码将我的数字四舍五入到小数点后两位.但是我得到这样的数字:10.8,2.4等.这些不是我的两位小数的想法,所以我如何改进以下内容?

Math.round(price*Math.pow(10,2))/Math.pow(10,2);
Run Code Online (Sandbox Code Playgroud)

我想要10.80,2.40等数字.使用jQuery对我很好.

javascript decimal-point rounding

557
推荐指数
8
解决办法
65万
查看次数

如何处理JavaScript中的浮点数精度?

我有以下虚拟测试脚本:

function test() {
  var x = 0.1 * 0.2;
  document.write(x);
}
test();
Run Code Online (Sandbox Code Playgroud)

这将打印结果0.020000000000000004,只应打印0.02(如果您使用计算器).据我所知,这是由于浮点乘法精度的误差.

有没有人有一个很好的解决方案,以便在这种情况下,我得到正确的结果0.02?我知道有类似的功能toFixed或舍入将是另一种可能性,但我想真正打印整个数字没有任何切割和舍入.只是想知道你们其中一个人是否有一些漂亮,优雅的解决方案.

当然,否则我会转到大约10位左右.

javascript floating-point

552
推荐指数
22
解决办法
34万
查看次数

Javascript toFixed不舍入

我正在使用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)

javascript

53
推荐指数
11
解决办法
7万
查看次数

Math.round(num)vs num.toFixed(0)和浏览器不一致

请考虑以下代码:

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)应该避免?将数字舍入到最接近的整数的正确方法是什么?

javascript cross-browser

51
推荐指数
4
解决办法
7万
查看次数

在javascript/jquery中舍入到3个小数点

我如何将margin_total舍入到3位小数?

margin_total = margin_total + parseFloat(marginObj.value);
document.getElementById('margin_total').value = margin_total;
Run Code Online (Sandbox Code Playgroud)

javascript

14
推荐指数
4
解决办法
3万
查看次数

Javascript VS PHP舍入

我对PHP和javascript轮数的方式有一些问题.我正在使用PHP的圆函数和这个javascript函数:

function roundNumber(number, decimals) {     
    var newnumber = new Number(number+'').toFixed(parseInt(decimals));
    var value = parseFloat(newnumber);
    return value;
}
Run Code Online (Sandbox Code Playgroud)

我试图舍入的数字是43.65*2.5 + 40%,当使用计算器= 152.775或在PHP = 152.78舍入时.

在javascript中我做一个console.log的数字是152.774999999998,当用上面的函数舍入时给我152.77

非常感谢任何帮助重新解决这个问题

javascript php

6
推荐指数
2
解决办法
4153
查看次数

Math.round()和.toFixed()中的舍入问题

我用了两种方法:

Number.prototype.myRound = function (decimalPlaces) {
    var multiplier = Math.pow(10, decimalPlaces);

    return (Math.round(this * multiplier) / multiplier);
};
alert((239.525).myRound(2));
Run Code Online (Sandbox Code Playgroud)

数学警报应该是239.53它的239.52输出.所以我尝试使用.toFixed()功能&我得到了正确的答案.

但是,当我试图得到239.575它的答案再次给出错误的输出.

alert((239.575).toFixed(2));
Run Code Online (Sandbox Code Playgroud)

这里输出应该是239.58它的给予239.57.

此错误在最终输出中产生一点差异.所以有人可以帮我解决这个问题吗?

javascript jquery

6
推荐指数
2
解决办法
8594
查看次数