Home >Backend Development >PHP Tutorial >PHP floor function problem.
$money=271.28; echo $money=floor($money*pow(10,2))/100I was surprised to find that the output was not 271.28 but 271.27!
Later I checked a lot of information and saw this on the php official website:
Later I printed the result of the above operation:
printf("%.12f", $money*pow(10,2));found that the result was: 27127.999999999996
It turned out to be an accuracy problem. No unified solution has been found yet. I can only add
if ($money - round ( $money, 2 ) < 0.00001) { }else{ $money=floor($money*pow(10,2))/100; }to the code logic to avoid reprocessing the data that has been accurate to two digits.
If anyone knows another way to implement the above function (accurate to two decimal places and discard whatever the third digit is), please let me know.
The above introduces the problem of PHP's floor function. , including relevant content, I hope it will be helpful to friends who are interested in PHP tutorials.