Home > Article > Backend Development > What should I do if php cannot be removed?
php solutions to endless divisions: 1. Use the round function to round floating point numbers; 2. Use the ceil method to achieve rounding; 3. Use the floor function to achieve rounding. .
#The operating environment of this article: Windows7 system, PHP7.1, Dell G3 computer.
php Division rounding
If we use the "/" operator to perform division operation, if we encounter a situation that cannot be divided, we will get a decimal value. What if I only want the integer part?
1.round — Rounds a floating-point number
float round ( float $val [, int $precision ] )
Returns val rounded to the specified precision (the number of decimal digits after the decimal point). precision can also be negative or zero (default).
//Example #1 round() 例子 <?php echo round(3.4); // 3 echo round(3.5); // 4 echo round(3.6); // 4 echo round(3.6, 0); // 4 echo round(1.95583, 2); // 1.96 echo round(1241757, -3); // 1242000 echo round(5.045, 2); // 5.05 echo round(5.055, 2); // 5.06 ?>
Note: PHP cannot handle strings like "12,300.2" correctly by default. See Convert String to Numeric.
Recommended: "PHP Video Tutorial"
2.ceil — Further rounding (rounding up)
float ceil ( float $value )
Returns no less than value The next integer of value, if value has a decimal part, add one digit. The type returned by ceil() is still float, because the range of float values is usually larger than that of integer.
//Example #1 ceil() 例子 <?php echo ceil(4.3); // 5 echo ceil(9.999); // 10 ?>
3.floor — Rounding by rounding method (rounding down)
float floor ( float $value )
Returns the next integer that is not greater than value, and rounds the decimal part of value. The type returned by floor() is still float, because the range of float values is usually larger than that of integer.
//Example #1 floor() 例子 <?php echo floor(4.3); // 4 echo floor(9.999); // 9 ?>
The above is the detailed content of What should I do if php cannot be removed?. For more information, please follow other related articles on the PHP Chinese website!