PHP round to the nearest whole number

Question

Answers ( 1 )

    0
    2024-01-11T18:05:20+00:00

    Here's an example of how you can use the round() function in PHP:

    <?php
      $number = 3.6;
      $roundedNumber = round($number);
      echo $roundedNumber;  // Outputs: 4
    ?>
    

    In this example, $number is set to 3.6. When you use round($number), it rounds the number to 4, because 3.6 is closer to 4 than to 3.

    PHP's round() function can also be used to specify the precision (number of decimal places) and mode of rounding, but for rounding to the nearest whole number, the default behavior of round() without additional parameters is sufficient.

Leave an answer