Home > Backend Development > PHP Tutorial > How to keep two decimal places and round or not round in PHP_PHP Tutorial

How to keep two decimal places and round or not round in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 16:12:45
Original
937 people have browsed it

PHP retains two decimal places and rounds

Copy code The code is as follows:

$num = 123213.666666;
echo sprintf( "%.2f", $num);

php retains two decimal places and does not round
Copy code The code is as follows:

$num = 123213.666666;
echo sprintf("%.2f",substr(sprintf("%.3f", $num), 0, -2));

php round to the nearest integer
Copy code The code is as follows:

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

PHP rounding method, take integer
Copy code The code is as follows:

echo floor(4.3); // 4
echo floor(9.999); // 9

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/313601.htmlTechArticlephp retains two decimal places and rounds the copy code as follows: $num = 123213.666666; echo sprintf("%.2f ", $num); php retains two decimal places and does not round. Copy the code as follows...
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