Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

PHP Dictionary

  1. Home
  2. PHP Dictionary
  3. round() / ceil() / floor()

round() / ceil() / floor() Since: PHP 4(2000)

Rounds, rounds up, rounds down, or gets the absolute value of a number. Useful in many situations such as monetary calculations and formatting numbers for display.

Syntax

// Rounds a number. The second argument specifies the number of decimal places.
round($number, $precision);

// Rounds up. If there is any fractional part, the result is the next integer.
ceil($number);

// Rounds down. Discards the fractional part and returns an integer.
floor($number);

// Returns the absolute value.
abs($number);

Function List

FunctionDescription
round($number, $precision)Rounds a number. The second argument specifies the number of decimal places to keep; if omitted, the result is rounded to the nearest integer. A negative value rounds to the tens, hundreds place, and so on.
ceil($number)Rounds a number up to the next integer. Even a tiny fractional part causes the result to increase to the next integer.
floor($number)Rounds a number down by discarding the fractional part entirely.
abs($number)Returns the absolute value of a number. Negative numbers are converted to positive; positive numbers and zero are returned unchanged.

Return Value

round() returns the rounded number — an integer if the argument is an integer, or a float if it is a float. ceil() and floor() always return a float. abs() returns the absolute value in the same type as the argument.

Sample Code

<?php
// Round with round().
echo round(3.5); // Outputs '4'.
echo round(3.4); // Outputs '3'.

// Specify the number of decimal places.
echo round(3.14159, 2); // Rounds to 2 decimal places and outputs '3.14'.
echo round(3.14159, 4); // Rounds to 4 decimal places and outputs '3.1416'.

// A negative precision rounds to the tens or hundreds place.
echo round(1234, -2); // Rounds to the hundreds place and outputs '1200'.
echo round(1250, -2); // Outputs '1300'.

// Round up with ceil().
echo ceil(4.1); // Outputs '5'.
echo ceil(4.9); // Outputs '5'.
echo ceil(-4.1); // For negative numbers, rounds toward zero and outputs '-4'.

// Practical example: calculate the total number of pages for pagination.
$total_items = 53;
$per_page = 10;
$total_pages = ceil($total_items / $per_page);
echo $total_pages; // Outputs '6'. Displaying 53 items at 10 per page requires 6 pages.

// Round down with floor().
echo floor(4.9); // Outputs '4'.
echo floor(-4.9); // For negative numbers, rounds away from zero and outputs '-5'.

// Get the absolute value with abs().
echo abs(-100); // Outputs '100'.
echo abs(100);  // Outputs '100'.

// Practical example: find the difference between two values.
$target = 50;
$actual = 73;
echo abs($target - $actual); // Always outputs a positive value, '23', regardless of which is larger.

Notes

round() rounds a number using the $precision argument. A positive value specifies the number of decimal places to keep; a negative value rounds to the tens or hundreds place. Floating-point arithmetic can introduce small errors, so consider using BCMath functions for calculations that require exact precision, such as monetary values.

ceil() and floor() may behave differently from what you expect with negative numbers. ceil() always rounds toward positive infinity, and floor() always rounds toward negative infinity. As a result, ceil(-4.1) returns -4 and floor(-4.9) returns -5.

abs() is useful for difference and distance calculations. To format numbers, use number_format(); to convert types, use intval().

If you find any errors or copyright issues, please .