Home  >  Article  >  Backend Development  >  How to retain two decimal places in php without rounding

How to retain two decimal places in php without rounding

coldplay.xixi
coldplay.xixiOriginal
2020-09-29 11:48:308797browse

php method to retain two decimal places without rounding: use the function substr, the code is [echo sprintf("%.2f",substr(sprintf("%.3f", $num), 0, -2))】.

How to retain two decimal places in php without rounding

php method of retaining two decimal places and not rounding:

php retaining two decimal places and not rounding

The code is as follows:

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

php rounding to an integer

The code is as follows:

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

php rounding to an integer

The code is as follows:

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

If you want to learn more about programming, please pay attention to the php training column!

The above is the detailed content of How to retain two decimal places in php without rounding. 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