How to keep 2 decimal places in php and not round

藏色散人
Release: 2023-03-12 12:54:01
Original
7849 people have browsed it

How to implement PHP to retain 2 decimal places and not round: 1. Create a PHP sample file; 2. Pass "sprintf("%.2f",substr(sprintf("%.3f", $num ), 0, -2));" method retains the specified number to 2 decimal places and does not round.

How to keep 2 decimal places in php and not round

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to retain 2 decimal places in php and not rounding?

PHP method of retaining two decimal places and rounding or not rounding

php retaining two decimal places and rounding

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

php retaining two decimal places decimal place and no rounding

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

php rounding to an integer

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

php rounding method, rounding to an integer

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

Recommended learning: "PHP Video Tutorial

The above is the detailed content of How to keep 2 decimal places in php and not round. 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