In PHP development, we often encounter the problem of decimal rounding. Previously, the editor shared several articles about several methods of rounding in PHP. Now we will analyze the method of PHP saving two decimal places and rounding.
php retains two decimal places and rounds
The code is as follows:
$num = 123213.666666;
echo sprintf("%.2f", $num);
php Keep two decimal places and do not round
The code is as follows:
$num = 123213.666666; echo sprintf("%.2f",substr(sprintf("%.3f", $num), 0, -2));
php Round to the nearest integer
The code is as follows:
echo ceil(4.3); // 5 echo ceil(9.999); // 10
php Rounding Go to the method and get the integer
The code is as follows:
echo floor(4.3); // 4 echo floor(9.999); // 9
Combined with the previous articles about PHP decimal point rounding, everyone should have a more intuitive understanding of PHP decimal rounding, and I also have a way of thinking to solve such problems. I believe that everyone will handle such problems well in the future.
Related recommendations:
php rounding function_PHP tutorial
PHP rounding to precise decimal places and rounding_PHP tutorial
php rounding interception floating point string method summary
The above is the detailed content of How to save two decimal places and round in php. For more information, please follow other related articles on the PHP Chinese website!