简体   繁体   中英

Using PHP how to round decimal number to ceil

I want to round 5.29 to 5.30, 5.14 to 5.15 how to do that I have tried this many ways but not working

Not working codes;

<?php
 echo round(5.29, 2);
 echo round((5.29*100)/100, 2); 
?>

please some one help me

If you were trying to round a decimal off to the nearest 0.05:

echo round(5.29 * 2, 1) / 2; // 5.30
echo round(5.14 * 2, 1) / 2; // 5.15

EDIT: Or if you wanted to round a decimal up to the nearest 0.05:

echo ceil(5.29 / 0.05) * 0.05; // 5.30
echo ceil(5.14 / 0.05) * 0.05; // 5.15

This code may help you

 $number=5.24;//number you want to round
 $decimal_part=($number-floor($number))*100;
 echo ceil($decimal_part/5)*5;

the above code will give you the required result. in above code if you give 5.21 it will give 5.25 . if you want 5.21 to convert into 5.20 then change the ceil to round

Please use following code

  echo (float)round(5.29 * 2, 1) / 2; // returns 5.30
  echo (float)round(5.14 * 2, 1) / 2 // returns 5.15

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM