Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
math
round
decimal
communitycreator

What is round() in PHP?

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

The round() function rounds off a number to a specified number of decimal places.

Figure 1 below shows a visual representation of the round() function.

Figure 1: Visual representation of the round() function

Syntax


round(num, ndigits, mode);

Parameters

This function requires three parameters.

  • It requires a number that is to be rounded off.

  • The ndigits parameter is the number of decimal places to which the above number needs to be rounded off. This is an optional parameter. Its default value is 0. If ndigits is omitted, then round() returns the nearest integer value of a number.

  • The mode parameter is used to decide the number of decimal places to round off. It is an optional parameter and its default value is PHP_ROUND_HALF_UP. Following are the available modes:

    • PHP_ROUND_HALF_UP: This mode rounds up the number to ndigits away from zero, when it is halfway there.
    • PHP_ROUND_HALF_DOWN: This mode rounds down the number to ndigits, when it is halfway there.
    • PHP_ROUND_HALF_EVEN: This mode rounds the number to ndigits, to the nearest even value.
    • PHP_ROUND_HALF_ODD: This mode rounds the number to ndigits, to the nearest odd value.

Return value

The round() function returns the number rounded off to the specified number of decimal places.

If you input a negative value (-n) in the ndigits, then n digits to the left of the decimal will be rounded off.

<?php
#round without ndigits and mode
echo("round(9.8923): ");
echo (round(9.8923));
echo("\n");
echo("round(-9.8923): ");
echo (round(-9.8923));
?>
<?php
#round with ndigits but without mode
echo("round(9.8923,2): ");
echo (round(9.8923,2));
echo("\n");
echo("round(-9.8923,2): ");
echo (round(-9.8923,2));
echo("\n");
echo("round(923.8923,-2): ");
echo (round(923.8923,-2));
echo("\n");
echo("round(-923.8923,-2): ");
echo (round(-923.8923,-2));
echo("\n");
?>
<?php
#round with ndigits and mode:PHP_ROUND_HALF_UP
echo("round(9.5,0,PHP_ROUND_HALF_UP): ");
echo (round(9.5,0,PHP_ROUND_HALF_UP));
echo("\n");
echo("round(-9.5,0,PHP_ROUND_HALF_UP): ");
echo (round(-9.5,0,PHP_ROUND_HALF_UP));
echo("\n");
?>
<?php
#round with ndigits and mode:PHP_ROUND_HALF_DOWN
echo("round(9.5,0,PHP_ROUND_HALF_DOWN): ");
echo (round(9.5,0,PHP_ROUND_HALF_DOWN));
echo("\n");
echo("round(-9.5,0,PHP_ROUND_HALF_DOWN): ");
echo (round(-9.5,0,PHP_ROUND_HALF_DOWN));
echo("\n");
?>
<?php
#round with ndigits and mode:PHP_ROUND_HALF_EVEN
echo("round(9.5,0,PHP_ROUND_HALF_EVEN): ");
echo (round(9.5,0,PHP_ROUND_HALF_EVEN));
echo("\n");
echo("round(-9.5,0,PHP_ROUND_HALF_EVEN): ");
echo (round(-9.5,0,PHP_ROUND_HALF_EVEN));
echo("\n");
?>
<?php
#round with ndigits and mode:PHP_ROUND_HALF_ODD
echo("round(9.5,0,PHP_ROUND_HALF_ODD): ");
echo (round(9.5,0,PHP_ROUND_HALF_ODD));
echo("\n");
echo("round(-9.5,0,PHP_ROUND_HALF_ODD): ");
echo (round(-9.5,0,PHP_ROUND_HALF_ODD));
echo("\n");
?>

RELATED TAGS

php
math
round
decimal
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring