round(): Rounding off variable values in PHP

Round a floating-point value

round() rounds a number to the requested precision. A precision of 2 is commonly used for display calculations involving two decimal places.

$value = 19.875;
echo round($value, 2); // 19.88

round() supports positive and negative precision. PHP 8.4+ also provides the RoundingMode enum for explicit rounding behavior.

$number=25.66745;
echo "The number = ". $number; // 25.66745 
echo "<br>Rounded value of the number = ".round($number,2); // 25.67 
echo "<br>Rounded value of the number = ".round($number); // 26 

Syntax

float round(int|float $num, int $precision = 0, int|RoundingMode $mode = RoundingMode::HalfAwayFromZero)

Positive precision rounds digits after the decimal point; negative precision rounds digits before it.

Use round() when you need a rounded numeric result. Use number_format() when you need a formatted string for display.

Use floor() to move toward negative infinity and ceil() to move toward positive infinity.

The table below compares the default round() behavior with floor() and ceil().

ValueFloor()ceil()round()
3.7344
3.4343
3.5344
3.48343
3.62344
2222
-8.7-9-8-9
-9.5-10-9-10
-9.3-10-9-9
-9.49-10-9-9
-9.61-10-9-10
-9-9-9-9

Explicit rounding modes in PHP 8.4+

var_dump(round(2.5, 0, RoundingMode::HalfEven));          // float(2)
var_dump(round(2.5, 0, RoundingMode::HalfAwayFromZero)); // float(3)

Example: Rounding prices

$price = 19.875;
echo round($price, 2);  // Output: 19.88

Example: Negative Precision Rounding

$num = 1246;
echo round($num, -2);  // Output: 1200

Math Functions max() function


Subscribe to our YouTube Channel here



plus2net.com







Sabby

17-05-2012

To the spot!! Thanks a lot!
krish

07-11-2013

if we use 11/3 answer is 3.67 but after we use addition 3.67+3.67+3.67 answer is 11.01
this is not a correct answer how to get right value please help me
smo

07-11-2013

When we round up the value we miss further fractions so we can't expect to get back the same value after adding them. Increase the decimal value to 4 digits and then see the result.
Johan H

03-02-2014

There is more round(), you can also force it to round down using floor() or round up (even if it's 10.0001 it'd round to 11) with ceil()
suchitra

09-11-2015

See i have to take the summed up value and then round it up, but round function not giving desired value.
$b=175+0+175+0+175+175+175+175+175+468.7
+0+175+468.7+468.7+468.7+689.8+175+0+175
+175+0+175+0+0+0+468.7+468.7+689.8+4
68.7+175+175;
echo"<br>".$b;
echo "<br>".round($b);

total = 7110.5
after rounding up it gives 7110 which is not desired value. We cannot use ceil function as every time it will give the higher value i.e 7.3=8
smo1234

10-04-2016

You can round each value and then sum or use number format and then add each value with decimal. The final value you can round off to check it matches your requirement or not.




PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer