0

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?

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
  • 1
    The '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 Answers1

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