Adding an integer to a float in PHP rounds a float to 1 decimal place

For example, I have this line of code:

echo 10359023529 + 0.2137582935;

Why is this coming back:

+10359023529.2

instead:

+10359023529,2137582935

+5
source share
3 answers

Using: bcadd()

echo bcadd(10359023529, 0.2137582935, 10);
+5
source

This is a floating point value limitation. The floating point value is stored as a coefficient and metric. You will have a lot of accuracy with small numbers and low accuracy with large numbers. Here is more detailed information than you would like: http://en.wikipedia.org/wiki/IEEE_754-2008

: . .

, PHP: http://php.net/manual/en/language.types.float.php

, BC (http://www.php.net/manual/en/ref.bc.php) GMP (http://www.php.net/manual/en/ref.gmp.php).

+3

php.ini , , .

; The number of significant digits displayed in floating point numbers.
; http://php.net/precision
precision = 14

, , , . IEEE 754 double 15.95 .

+1

All Articles