Home >Backend Development >PHP Problem >How to keep 2 decimal places in php and not round

How to keep 2 decimal places in php and not round

藏色散人
藏色散人Original
2021-09-11 09:50:357910browse

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);

php retaining two decimal places decimal place and no rounding

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

php rounding to an integer

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

php rounding method, rounding to an integer

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

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!

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