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

How to retain two decimal places in php without rounding

coldplay.xixi
Release: 2023-03-06 10:04:01
Original
8841 people have browsed it

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));
Copy after login

php rounding to an integer

The code is as follows:

echo ceil(4.3); // 5
echo ceil(9.999); // 10
Copy after login

php rounding to an integer

The code is as follows:

echo floor(4.3); // 4
echo floor(9.999); // 9
Copy after login

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!

Related labels:
php
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