Search code examples
phpfloating-pointdecimal-pointrounding

PHP: rounding a number into 16 decimal digits


Hi I'm trying to rounding a number into the 16 decimal digits but it only show and doesn't round up till 14 decimal digit.

Here's my attempt:

<?php

$num= 0.16346153846153846;

$round = number_format((float)$num, 17, '.', '');

echo $round * -1;
?>

OUTPUT:

-0.16346153846154

EXPECTED OUTPUT:

0.1634615384615385

I know that float is only 14 decimal digits. Is there any other way around for the 16 decimal digits?


Solution

  • You can set this parameters at run-time. 16 digits is usually the maximum value on most platforms, larger values giving only meaningless or "fictional" digits:

    ini_set("precision", "16");
    

    See Changing precision level floating-point variables