Home > Backend Development > PHP Tutorial > How to round down a floating point number using the floor function in PHP

How to round down a floating point number using the floor function in PHP

王林
Release: 2023-06-26 13:50:02
Original
2296 people have browsed it

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.

  1. Round positive numbers

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;
Copy after login

The output result is:

原始值:3.8
向下取整值:3
Copy after login
  1. Rounding negative numbers

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;
Copy after login

The output result is:

原始值:-4.5
向下取整值:-5
Copy after login
  1. Rounding decimals

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;
Copy after login

The output result is:

原始值:2.3642875
向下取整值:2.36
Copy after login
  1. Rounding to one hundred and ten digits

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;
Copy after login

The output result is:

原始值:123.456789
向下取整值:100
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template