Home  >  Article  >  Backend Development  >  What is the php division function?

What is the php division function?

藏色散人
藏色散人Original
2020-08-28 10:16:334651browse

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));
?>

Output:

1
1
0
-4
-5

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));
?>

Output:

1
1
5
6
-5
-5

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))
?>

Output:

0
0
5
5
-6
-6

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!

Statement:
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
Previous article:What is smarty php methodNext article:What is smarty php method