Home  >  Article  >  Backend Development  >  php floating point number rounding function

php floating point number rounding function

高洛峰
高洛峰Original
2016-11-29 11:41:291236browse

This article introduces two commonly used functions round in PHP for rounding floating point numbers. At the same time, the ceil function may retain the number of decimal places.

round() function rounds floating point numbers

Syntax: float round ( float val [,int precision] )

Returns the result of rounding val according to the specified precision precision (the number of digits after the decimal point). precision can also be a negative number or zero (default value).

round() example, the code is as follows:

echo round(3.4); 3

echo round(3.5); / 4

echo round(3.6, 0); / / 4

echo round (1.95583, 2); 2); / / 5.05

echo round(5.055, 2); // 5.06

?>

ceil -- Rounding to the nearest integer

Explanation: float ceil (float value)

Returns the next integer that is not less than value , If the value has a decimal part, it is 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 1. ceil() example, the code is as follows:

echo ceil(4.3); // 5

echo ceil(9.999); // 10

?>

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