I am having a problem with php conversion from string to float
In some cases, if I do:
floatval("8.80")
I get:
8.800000000000001
I have struggled with round(x,1), number_format, etc, but to no avail
What am I getting wrong here?
I am having a problem with php conversion from string to float
In some cases, if I do:
floatval("8.80")
I get:
8.800000000000001
I have struggled with round(x,1), number_format, etc, but to no avail
What am I getting wrong here?
By using number_format will help to resolve your issue
<?php
$number = 8.800000000000001;
$precision = 1;
$number = intval($number * ($p = pow(10, $precision))) / $p;
echo number_format((float) $number, $precision, '.', '');
?>
I have same issuer, cast in string with number_format ou bcmath library work for display. But if you do a calcul, same problem.
<?php
$number = 8.800000000000001;
$precision = 1;
$number = intval($number * ($p = pow(10, $precision))) / $p;
echo (float)number_format((float) $number, $precision, '.', '');
?>
This give the same bad result
I found the solution. You need to set the "precision" PHP variable ("serialize_precision" pour php8) to 14 or more
ini_set("precision",17);
or
ini_set("serialize_precision",17);