Home > Backend Development > PHP Tutorial > How to save two decimal places and round in php

How to save two decimal places and round in php

小云云
Release: 2023-03-17 12:34:01
Original
2914 people have browsed it

In PHP development, we often encounter the problem of decimal rounding. Previously, the editor shared several articles about several methods of rounding in PHP. Now we will analyze the method of PHP saving two decimal places and rounding.

php retains two decimal places and rounds
The code is as follows:
$num = 123213.666666;
echo sprintf("%.2f", $num);

php Keep two decimal places and do not round
The code is as follows:

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

php Round to the nearest integer

The code is as follows:

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

php Rounding Go to the method and get the integer

The code is as follows:

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

Combined with the previous articles about PHP decimal point rounding, everyone should have a more intuitive understanding of PHP decimal rounding, and I also have a way of thinking to solve such problems. I believe that everyone will handle such problems well in the future.

Related recommendations:

php rounding function_PHP tutorial

PHP rounding to precise decimal places and rounding_PHP tutorial

php rounding interception floating point string method summary

The above is the detailed content of How to save two decimal places and round in php. For more information, please follow other related articles on the PHP Chinese website!

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