PHP method of reserving two decimal places and rounding or not rounding

  • 2020-09-28 08:49:47
  • OfStack

php retains two decimal places and rounds 4 to 5
 
$num = 123213.666666; 
echo sprintf("%.2f", $num); 

php retains two decimal places and does not round 4 to 5
 
$num = 123213.666666; 
echo sprintf("%.2f",substr(sprintf("%.3f", $num), 0, -2)); 

php round off with 1
 
echo ceil(4.3); // 5 
echo ceil(9.999); // 10 

php, leave it out, take an integer
 
echo floor(4.3); // 4 
echo floor(9.999); // 9 

Related articles: