PHP round() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

In PHP, round() rounds a number to the nearest whole number or chosen decimal places.

By default, numbers ending in .5 round up (away from zero). You can also round to tens or hundreds with a negative precision, or change the rounding mode.

Syntax

Call round() with the values you need. Start with this line.

PHP
$rounded = round(3.14159, 2);
CallWhat it does
round(3.14159, 2)Returns 3.14.
round(2.5)Returns 3, because .5 rounds up by default.
round(1234.5678, -2)Returns 1200, rounded to the nearest hundred.

round() always returns a float, even when the result is a whole number. For display with thousand separators, use number_format(). To always round down or up, use floor() or ceil().

Library declarations
PHP
round(int|float $num, int $precision = 0, int $mode = PHP_ROUND_HALF_UP): float

Parameters

ParameterDescription
numRequired. The number to round.
precisionOptional. The number of decimal places. The default is 0. A negative value rounds to the left of the decimal point.
modeOptional. A rounding mode constant: PHP_ROUND_HALF_UP (default), PHP_ROUND_HALF_DOWN, PHP_ROUND_HALF_EVEN, or PHP_ROUND_HALF_ODD.

Return Value

Return ValueDescription
floatThe rounded number as a float.

PHP Examples

Example 1 — Round to Decimal Places

Pass the number first and the number of decimals second.

PHP
<?php
echo round(3.14159, 2), PHP_EOL;
echo round(2.5), PHP_EOL;
echo round(-2.5), PHP_EOL;
echo round(1.95583, 2), PHP_EOL;

How It Works

  1. round(3.14159, 2) keeps two decimals and returns 3.14.
  2. round(2.5) rounds half up to 3.
  3. round(-2.5) rounds away from zero to -3.
  4. round(1.95583, 2) looks at the third decimal (5) and rounds up to 1.96.

Example 2 — Negative Precision

A negative precision rounds to tens, hundreds, or thousands.

PHP
<?php
echo round(1234.5678, -2), PHP_EOL;
echo round(1241757, -3), PHP_EOL;
echo round(5.5), PHP_EOL;

How It Works

  1. A precision of -2 rounds to the nearest hundred, so 1234.5678 becomes 1200.
  2. A precision of -3 rounds to the nearest thousand, so 1241757 becomes 1242000.
  3. With no precision, 5.5 rounds up to 6.
  4. The results are floats, but they print without decimals.

Example 3 — Use Rounding Modes

The third argument changes how exact halves are rounded.

PHP
<?php
echo round(2.5, 0, PHP_ROUND_HALF_UP), PHP_EOL;
echo round(2.5, 0, PHP_ROUND_HALF_DOWN), PHP_EOL;
echo round(2.5, 0, PHP_ROUND_HALF_EVEN), PHP_EOL;
echo round(2.5, 0, PHP_ROUND_HALF_ODD), PHP_EOL;

How It Works

  1. PHP_ROUND_HALF_UP rounds 2.5 away from zero to 3.
  2. PHP_ROUND_HALF_DOWN rounds 2.5 toward zero to 2.
  3. PHP_ROUND_HALF_EVEN rounds to the nearest even number, which is 2.
  4. PHP_ROUND_HALF_ODD rounds to the nearest odd number, which is 3.

Frequently Asked Questions

round() rounds a number to the nearest whole number, or to the number of decimal places you choose.
Use round($number, 2). For example, round(3.14159, 2) returns 3.14.
Yes. Even round(5.2) returns the float 5. Cast with (int) if you need an integer.
round() rounds to the nearest value. Use ceil() to always round up and floor() to always round down.
Use a negative precision. round(1234, -1) gives 1230 and round(1234, -2) gives 1200.

Next: sin()

sin() returns the sine of an angle given in radians.

sin() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful