How to implement PHP to retain 2 decimal places and not round: 1. Create a PHP sample file; 2. Pass "sprintf("%.2f",substr(sprintf("%.3f", $num ), 0, -2));" method retains the specified number to 2 decimal places and does not round.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
How to retain 2 decimal places in php and not rounding?
PHP method of retaining two decimal places and rounding or not rounding
php retaining two decimal places and rounding
$num = 123213.666666; echo sprintf("%.2f", $num);
php retaining two decimal places decimal place and no rounding
$num = 123213.666666; echo sprintf("%.2f",substr(sprintf("%.3f", $num), 0, -2));
php rounding to an integer
echo ceil(4.3); // 5 echo ceil(9.999); // 10
php rounding method, rounding to an integer
echo floor(4.3); // 4 echo floor(9.999); // 9
Recommended learning: "PHP Video Tutorial》
The above is the detailed content of How to keep 2 decimal places in php and not round. For more information, please follow other related articles on the PHP Chinese website!