Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dealing with float precision in Javascript [duplicate]

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

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 
like image 932
Jeroen Ooms Avatar asked Jul 27 '12 21:07

Jeroen Ooms


People also ask

Does double have more precision than float?

double has 2x more precision than float. float is a 32-bit IEEE 754 single precision Floating Point Number – 1 bit for the sign, 8 bits for the exponent, and 23* for the value. float has 7 decimal digits of precision.

What is float precision JavaScript?

The representation of floating points in JavaScript follows the IEEE-754 format. It is a double precision format where 64 bits are allocated for every floating point.

Is float double precision?

A variable of type float only has 7 digits of precision whereas a variable of type double has 15 digits of precision. If you need better accuracy, use double instead of float.


2 Answers

From this post: How to deal with floating point number precision in 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
like image 110
Rusty Fausak Avatar answered Oct 17 '22 20:10

Rusty Fausak


You could do something like this:

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

Edit: It would be better to use big.js.

big.js

A small, fast, easy-to-use library for arbitrary-precision decimal arithmetic.

>> bigX = new Big(x) >> bigY = new Big(y) >> bigY.div(bigX).round().times(bigX).toNumber() // => 1.2 
like image 43
philipvr Avatar answered Oct 17 '22 19:10

philipvr