Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP to round up to the 2nd decimal place

Tags:

php

rounding

By calculating areas I have a number which I need to display in a strange way.

Always display 2 decimal places. Always round up the 2nd decimal place if the 3rd+ decimal places > 0.

Examples:

0.5 = 0.50
0.500003 = 0.51
0.96531 = 0.97
0.96231 = 0.97
0.8701 = 0.88

Is there a built in function to do this in PHP or do I need to write one?

like image 401
Miklos Avatar asked Sep 17 '25 22:09

Miklos


1 Answers

To always round up you will want to use something like this:

$number = 0.8701;

echo ceil($number*100)/100;

// = 0.88
like image 57
Paul Blundell Avatar answered Sep 20 '25 11:09

Paul Blundell