Home > Backend Development > PHP Problem > What is the php division function?

What is the php division function?

藏色散人
Release: 2023-03-05 11:24:02
Original
4715 people have browsed it

There are 3 PHP division functions, namely: 1. round function, used to round floating point numbers; 2. ceil function, used to round up to the nearest integer; 3. floor function, used Rounded down to the nearest integer.

What is the php division function?

Recommended: "PHP Video Tutorial"

How to divide and round in php (round, ceil, floor)

When encountering a situation where the result of division needs to be rounded in PHP, the following methods need to be used:

1. round: rounding

round() function rounds floating point numbers.

Syntax: round(x, prec)

Parameters

x is optional. Specifies the number to be rounded.

prec Optional. Specifies the number of digits after the decimal point.

Description: Returns the result of rounding x according to the specified precision prec (the number of decimal digits after the decimal point). prec can also be negative or zero (default).

Tip: PHP cannot handle strings like "12,300.2" correctly by default.

Example:

<?php
echo(round(0.60));
echo(round(0.50));
echo(round(0.49));
echo(round(-4.40));
echo(round(-4.60));
?>
Copy after login

Output:

1
1
0
-4
-5
Copy after login

2. ceil: Round up

ceil() function rounds up to the nearest integer.

Syntax: ceil(x)

Parameters

x is required. Specifies the number to be rounded.

Description: Return the next integer that is not less than x. If x has a decimal part, it will be rounded up by one. The type returned by ceil() is still float because the range of float values ​​is usually larger than that of integer.

Example:

<?php
echo(ceil(0.60);
echo(ceil(0.40);
echo(ceil(5);
echo(ceil(5.1);
echo(ceil(-5.1);
echo(ceil(-5.9));
?>
Copy after login

Output:

1
1
5
6
-5
-5
Copy after login

3. floor: round down

floor() function rounds down to the nearest integer.

Syntax: floor(x)

Parameters

x is required. Specifies the number to be rounded.

Description: Return the next integer not greater than x, and round off the decimal part of x. The type returned by floor() is still float because the range of float values ​​is usually larger than that of integer.

Example:

<?php
echo(floor(0.60));
echo(floor(0.40));
echo(floor(5));
echo(floor(5.1));
echo(floor(-5.1));
echo(floor(-5.9))
?>
Copy after login

Output:

0
0
5
5
-6
-6
Copy after login

The above is the detailed content of What is the php division function?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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