Home  >  Article  >  Backend Development  >  PHP floating point number rounding function_PHP tutorial

PHP floating point number rounding function_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:03:161443browse

This article introduces two commonly used functions for rounding floating point numbers in PHP: round and the ceil function, which may retain the number of decimal places. ​

The round() function rounds floating point numbers

Grammar

float round ( float val [, int precision] )

Returns val rounded to the specified precision (the number of decimal digits after the decimal point). precision can also be negative or zero (default).


round() example

The code is as follows Copy code
 代码如下 复制代码

echo round(3.4);             // 3
echo round(3.5);             // 4
echo round(3.6);             // 4
echo round(3.6, 0);         // 4
echo round(1.95583, 2);     // 1.96
echo round(1241757, -3); // 1242000
echo round(5.045, 2);     // 5.05
echo round(5.055, 2);     // 5.06
?>

echo round(3.4); // 3

echo round(3.5);                              // 4
echo round(3.6);                               // 4
echo round(3.6, 0); // 4
echo round(1.95583, 2); // 1.96

echo round(1241757, -3); // 1242000

echo round(5.045, 2); // 5.05

echo round(5.055, 2); // 5.06
 代码如下 复制代码

echo ceil(4.3);     // 5
echo ceil(9.999);     // 10
?>

?>
ceil -- rounding to the next integer

Description

float ceil (float value) Example 1. ceil() example
The code is as follows Copy code
echo ceil(4.3); // 5 echo ceil(9.999); // 10 ?>
http://www.bkjia.com/PHPjc/445302.htmlwww.bkjia.com
truehttp: //www.bkjia.com/PHPjc/445302.htmlTechArticleThis article introduces two commonly used functions for rounding floating point numbers in php, round and the ceil function, which may Number of decimal places to keep. The round() function rounds floating point numbers. Syntax...
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