I have a scenario here where I need to perform a few calculations and I need the exact float number not the rounded float number. For e.g this is the float I want 51.8385922330097
, which rounds of to 51.83859223301
. So any idea how to prevent this auto round of in PHP?
Asked
Active
Viewed 531 times
0

Hammad Rasheed
- 241
- 1
- 7
-
Short answer: That's how [floats work](https://en.wikipedia.org/wiki/IEEE_754), deal with it. Long answer: Use an [arbitrary precision library](https://php-decimal.io/#introduction). – tadman Feb 20 '21 at 10:54
-
1The 'exact float number' is a myth born of of the workings of floating point representation. What you need is [BCMath](https://www.php.net/manual/en/book.bc.php) – Tangentially Perpendicular Feb 20 '21 at 10:56
1 Answers
1
Floating point numbers have a limited accuracy, you shoudl check this PHP Doc article: https://www.php.net/manual/en/language.types.float.php
This might help you: How can use big float number in PHP?
If you wish to dive into the scientific reasons, this is a pretty good explanation: Why are floating point numbers inaccurate?

Robert Fiko
- 124
- 5
-
Increasing `precision` value in `php.ini` worked for now. ini_set('precision', 16); – Hammad Rasheed Feb 20 '21 at 12:36