Javascript float precision handling

I have a large number of numeric y values ​​in javascript. I want to group them by rounding them to the nearest multiple x and converting the result to a string.

How do I get around annoying floating point precision?

For example:

 0.2 + 0.4 = 0.6000000000000001 

Two things I've 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 
+74
javascript double floating-point floating-accuracy numerical-methods
Jul 27 2018-12-12T00:
source share
5 answers

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

You have several options:

  • Use a special data type for decimal numbers , for example, decimal.js
  • Format your result, (Math.floor(y/x) * x).toFixed(2) fixed number of significant digits, for example: (Math.floor(y/x) * x).toFixed(2)
  • Convert all your numbers to integers
+105
Jul 27 '12 at 21:14
source share

You could do something like this:

 > +(Math.floor(y/x)*x).toFixed(15); 1.2 
+4
Jul 27 '12 at 9:15
source share
 > var x = 0.1 > var y = 0.2 > var cf = 10 > x * y 0.020000000000000004 > (x * cf) * (y * cf) / (cf * cf) 0.02 

Fast decision:

 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 description here .

+4
Mar 19 '14 at 2:07
source share

Check out this link. It helped me a lot.

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

The toPrecision(no_of_digits_required) function returns a string so remember to use parseFloat() to convert to decimal point the required precision.

+1
Apr 09 '13 at 15:01
source share

Solving this problem, I will first find the number of decimal places in x , then round y respectively. I would use:

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

It should convert x to a string, split it into a decimal point, find the length of the right-hand side, and then y.toFixed(length) round y based on that length.

0
Jul 27 '12 at 21:14
source share



All Articles