处理 Javascript 中的浮点精度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11695618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:45:59  来源:igfitidea点击:

Dealing with float precision in Javascript

javascriptfloating-pointdoublefloating-accuracynumerical-methods

提问by Jeroen

I have a large amount of numeric values yin javascript. I want to group them by rounding them down to the nearest multiple of xand convert the result to a string.

y在 javascript 中有大量的数值。我想通过将它们四舍五入到最接近的倍数来对它们进行分组x,并将结果转换为字符串。

How do I get around the annoying floating point precision?

如何解决烦人的浮点精度?

For example:

例如:

0.2 + 0.4 = 0.6000000000000001

Two things I have tried:

我尝试过的两件事:

>>> y = 1.23456789 
>>> x = 0.2 
>>> parseInt(Math.round(Math.floor(y/x))) * x; 
1.2000000000000002

and:

和:

>>> y = 1.23456789 
>>> x = 0.2 
>>> y - (y % x)
1.2000000000000002

回答by Rusty Fausak

From this post: How to deal with floating point number precision in JavaScript?

来自这篇文章:如何处理 JavaScript 中的浮点数精度?

You have a few options:

您有几个选择:

  • Use a special datatype for decimals, like decimal.js
  • Format your result to some fixed number of significant digits, like this: (Math.floor(y/x) * x).toFixed(2)
  • Convert all your numbers to integers
  • 使用特殊的小数数据类型,如decimal.js
  • 将结果格式化为一些固定数量的有效数字,如下所示: (Math.floor(y/x) * x).toFixed(2)
  • 将所有数字转换为整数

回答by philipvr

You could do something like this:

你可以这样做:

> +(Math.floor(y/x)*x).toFixed(15);
1.2

回答by Hamza Alayed

> var x = 0.1
> var y = 0.2
> var cf = 10
> x * y
0.020000000000000004
> (x * cf) * (y * cf) / (cf * cf)
0.02

Quick solution:

快速解决方案:

var _cf = (function() {
  function _shift(x) {
    var parts = x.toString().split('.');
    return (parts.length < 2) ? 1 : Math.pow(10, parts[1].length);
  }
  return function() { 
    return Array.prototype.reduce.call(arguments, function (prev, next) { return prev === undefined || next === undefined ? undefined : Math.max(prev, _shift (next)); }, -Infinity);
  };
})();

Math.a = function () {
  var f = _cf.apply(null, arguments); if(f === undefined) return undefined;
  function cb(x, y, i, o) { return x + f * y; }
  return Array.prototype.reduce.call(arguments, cb, 0) / f;
};

Math.s = function (l,r) { var f = _cf(l,r); return (l * f - r * f) / f; };

Math.m = function () {
  var f = _cf.apply(null, arguments);
  function cb(x, y, i, o) { return (x*f) * (y*f) / (f * f); }
  return Array.prototype.reduce.call(arguments, cb, 1);
};

Math.d = function (l,r) { var f = _cf(l,r); return (l * f) / (r * f); };

> Math.m(0.1, 0.2)
0.02

You can check the full explanation here.

您可以在此处查看完整说明。

回答by prahaladp

Check out this link.. It helped me a lot.

看看这个链接..它帮了我很多。

http://www.w3schools.com/jsref/jsref_toprecision.asp

http://www.w3schools.com/jsref/jsref_toprecision.asp

The toPrecision(no_of_digits_required)function returns a stringso don't forget to use the parseFloat()function to convert to decimal point of required precision.

toPrecision(no_of_digits_required)函数返回一个string所以不要忘记使用该parseFloat()函数转换为所需精度的小数点。

回答by whaatt

Tackling this task, I'd first find the number of decimal places in x, then round yaccordingly. I'd use:

处理这个任务时,我会先找到 中的小数位数x,然后y相应地四舍五入。我会用:

y.toFixed(x.toString().split(".")[1].length);

It should convert xto a string, split it over the decimal point, find the length of the right part, and then y.toFixed(length)should round ybased on that length.

它应该转换x为一个字符串,将其拆分为小数点,找到正确部分的长度,然后根据该长度进行y.toFixed(length)舍入y