1

I need to use numbers with 16 decimal places in a project and do math operations with them, but I have problems with their handling.

Whatever it does in php, these are truncated if they are too long. such as if:

$value = 123456789.1234567890;

Printing it with "echo" the result I have on the screen is this: 123456789.12346

Even if the number has few elements in the integer part, the decimal part is truncated

$value = 0.12345678901234567890;

In output I have:

0.12345678901235

What can I do to manage them easily?

Thanks for your help.

I tried using the instruction suggested by GuidoFaecke:

ini_set('precision', '16')

Or:

ini_set('precision', '-1');

But when I use -1 I don't see decimal number. Using 16 I don't see changing showing the number.

  • You could try `ini_set('precision', '16');` or `ini_set('precision', '-1');` – Guido Faecke Oct 19 '22 at 17:28
  • @GuidoFaecke I tried with this istruction. But it doesn't seem to work in my case. – Goffredo psw Oct 19 '22 at 18:00
  • Does this answer your question? [Can I rely on PHP php.ini precision workaround for floating point issue](https://stackoverflow.com/questions/14587290/can-i-rely-on-php-php-ini-precision-workaround-for-floating-point-issue) – nice_dev Oct 19 '22 at 18:32
  • The commonly used floating-point format IEEE-754 binary64 does not have enough precision to uniquely distinguish all 16-decimal-digit numbers in its range. You could use 64-bit integer arithmetic and convert the results to decimal form only for display. – Eric Postpischil Oct 19 '22 at 20:41
  • 1. Use [BCmath](https://www.php.net/manual/en/book.bc.php). 2. [You can't possibly need that many digits.](https://www.jpl.nasa.gov/edu/news/2016/3/16/how-many-decimals-of-pi-do-we-really-need/) – Sammitch Oct 19 '22 at 22:33
  • @Sammitch: Arithmetic is not always used to model physics and, even when it is, not all of the numbers used are necessarily direct representations of physical quantities. Cryptography, for example, needs to do arithmetic with numbers larger than 16 digits in order to mix the information. Arithmetic software may also need to support high precision because it is used sometimes with large numbers and sometimes with small numbers and has to accommodate both even though the relative precision needed in any one instance is lower. So, yes, people can possibly need that many digits. – Eric Postpischil Oct 20 '22 at 09:20

0 Answers0