Google News
logo
PHP Program to Round a floating-point number to its nearest integer
In PHP, you can use the `round()` function to round a floating-point number to its nearest integer. The `round()` function takes one argument, which is the number to be rounded.

Here's an example :
Program :
<?php
$number = 3.7;
$rounded_number = round($number);

echo "The rounded number is $rounded_number\n";
?>
Output :
The rounded number is 4
If the number is exactly halfway between two integers, the `round()` function will round it to the nearest even integer. This is known as "banker's rounding". For example :
Program :
<?php
$number = 2.5;
$rounded_number = round($number);

echo "The rounded number is $rounded_number\n";
?>
Output :
The rounded number is 2
If you want to always round up or down, regardless of whether the number is exactly halfway between two integers, you can use the `ceil()` or `floor()` functions, respectively. For example :
<?php
$number = 2.5;
$rounded_up = ceil($number);
$rounded_down = floor($number);

echo "The rounded up number is $rounded_up\n";
echo "The rounded down number is $rounded_down\n";
?>​

Output :
The rounded up number is 3
The rounded down number is 2