Home » Programming » PHP » Php Round

How to Round Numbers (Integer, Decimals) in PHP with the round() Function

This article will show you how to round numbers in PHP with the round() function, including rounding to the nearest integer or a set number of decimal places.

Why Round Numbers?

There are various reasons you may want to round numbers. You don’t need incredible accuracy for many day-to-day calculations. You probably don’t need to know your height to several thousandths of a centimeter if converting from feet – knowing to the nearest whole centimeter is perhaps enough. If you’re calculating tax, you only need to know the result to the smallest unit of currency available – a certain number of fixed decimal places.

A number with accuracy beyond its intended purpose adds confusion and affects readability – rounding lets you appropriately represent that value.

PHP round() Function Syntax

The PHP round() function has the following syntax:

?round($number, $precision, $mode)

Note that:

  • $number can be any number value
  • $precision is the number of digits you wish to appear after the decimal point in the resulting number
    • If not specified, the $precision will be set to 0, i.e., rounding to the nearest integer
    • If a negative precision is given, the number of digits before the decimal point is rounded to the nearest power of 10
      • i.e., if $precision is -1, rounding is done to the nearest 10; if it is -2, the nearest 100
  • $mode defines how the number is rounded
    • See the below table for options
    • This value is optional and will default to PHP_ROUND_HALF_UP
  • Remember, rounding is a one-way thing – you can’t get the original number back from a rounded number – so if the original number is still required, make sure you assign the rounded number to a new variable

Rounding modes:

PHP_ROUND_HALF_UP Rounds away from zero for numbers half or greater. 2.5 becomes 3, -2.5 becomes -3
PHP_ROUND_HALF_DOWN Rounds towards zero for numbers half or lower. 2.5 becomes 2, -2.5 becomes -2
PHP_ROUND_HALF_EVEN Rounds to the nearest even number when at the half point. 1.5 becomes 2, 2.5 becomes 2
PHP_ROUND_HALF_ODD Rounds to the nearest odd number when at the half point. *1.5 becomes 1, 2.5 becomes 3

Examples

Rounding to Integer

In the below example, a floating-point number is defined and then rounded to the nearest integer:

$myNumber = 3.56;
$myRoundedNumber = round($myNumber);
echo $myRoundedNumber; // Will output the number 4

As no precision is defined, it has defaulted to 0.

Rounding to Decimals

Below, we round to 1 decimal place by specifying a precision:

$myNumber = 3.56;
$myRoundedNumber = round($myNumber, 1);
echo $myRoundedNumber; // Will output the number 3.6

Rounding to Nearest Even or Odd Number

The below example rounds to the nearest even and odd digit at two decimal places. When at the halfway point of rounding, either the nearest odd or even digit is used:

$myNumber = 3.565;
$myRoundedNumberEven = round($myNumber, 2, PHP_ROUND_HALF_EVEN);
$myRoundedNumberOdd = round($myNumber, 2, PHP_ROUND_HALF_ODD);
echo $myRoundedNumberEven; // Will output the number 3.56
echo "\n";// Print new line
echo $myRoundedNumberOdd; // Will output the number 3.57

 

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment