In PHP, there is a very practical function - the floor() function, which can round a floating point number down to the nearest integer. For example, for a floating point number 2.6, the floor() function will round it down to 2, and for another floating point number -2.6, the floor() function will round it down to -3. In some cases, we may need this function to handle floating point numbers. How to use the floor() function will be explained in detail below.
The syntax format of the floor() function is as follows:
float floor (float $value)
where $value is the floating point number that needs to be rounded down.
First, let’s look at the simplest example: how to use the floor() function to round a floating point number down to the nearest Is it an integer? The following is a sample code:
$input_num = 3.8; $output_num = floor($input_num); echo "原始值:".$input_num."<br>"; echo "向下取整值:".$output_num;
The output result is:
原始值:3.8 向下取整值:3
If you need to round down a negative number, you can directly Pass a negative number into the floor() function, and the floor() function will automatically round it down to the nearest integer.
$input_num = -4.5; $output_num = floor($input_num); echo "原始值:".$input_num."<br>"; echo "向下取整值:".$output_num;
The output result is:
原始值:-4.5 向下取整值:-5
In addition to integers and negative numbers, we can also use the floor() function to round decimals Round down. Let’s look at an example, rounding a floating point number down to two decimal places:
$input_num = 2.3642875; $output_num = floor($input_num * 100) / 100; //将该数乘以100后,再除以100,即可取到小数点后两位 echo "原始值:".$input_num."<br>"; echo "向下取整值:".$output_num;
The output result is:
原始值:2.3642875 向下取整值:2.36
Sometimes, we need to round floating point numbers down to hundreds of digits. You can consider using the floor() function. The following is a sample code:
$input_num = 123.456789; $output_num = floor($input_num / 100) * 100; //将该数除以100后,再乘以100即可向下取整到百十个位 echo "原始值:".$input_num."<br>"; echo "向下取整值:".$output_num;
The output result is:
原始值:123.456789 向下取整值:100
In addition to the above four situations, there are other situations where the floor() function can be used, but it needs to be adjusted according to specific needs to achieve the most appropriate effect.
Summary
The floor() function is a very practical function that can round a floating point number down to the nearest integer. In PHP, using the floor() function is very simple. You only need to pass the floating point number to be processed into the function. At the same time, you can also customize the way to round the floating point number, such as rounding the floating point number to two decimal places. digits, hundred and ten digits, etc. It is worth mentioning that there is also the ceil() function in PHP, which can round a floating-point number up to the nearest integer. The usage method is very similar to the floor() function.
The above is the detailed content of How to round down a floating point number using the floor function in PHP. For more information, please follow other related articles on the PHP Chinese website!