PHP rounding, rounding, use of round function

不言
Release: 2023-03-30 06:18:02
Original
2377 people have browsed it

This article mainly introduces PHP rounding, rounding, and round function usage examples. Friends in need can refer to the

decimal examples:

PHP retains two decimal places and Rounding

Copy codeThe code is as follows:

$n=0.1265489; echo sprintf("%.2f", $n); // 0.13
Copy after login

You can see that we used the sprintf function to format $n%.2f is the target format , where 2 represents two digits and f represents float (floating point type). The 3rd is the decimal 6 and is rounded off.
Let’s look at another example.

Copy codeThe code is as follows:

$n=0.1265489 echo substr(sprintf("%.3",$n),0,-1);// 0.12
Copy after login

The code output retains 2 as a decimal without rounding. In fact, we understand that the characteristics of sprintf will round the decimal and we retain one more digit, and then use substr to intercept the first 2 digits

Rounding Example:

Copy codeThe code is as follows:

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

The ceil function is an upward rounding function. What does upward mean? That is to say, if it exceeds a little bit, move forward one bit. For example, 4.1 becomes 5 in the example.

On the contrary, there is a function called floor. Let’s take a look at its usage.

Copy codeThe code is as follows:

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

The characteristics of floor are in Chapter 1 The two outputs are particularly obvious, that is, without giving you how many decimal places, even if it is infinitely close to 10, the integer you get by going down is 9.

round function

Copy codeThe code is as follows:

Copy after login

The description of the round function in the PHP manual is:

float round (float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]] )
Returns the result of rounding val according to the specified precision (the number of digits after the decimal point). precision can also be negative or zero (default).

#The first parameter of round is the data source, the second parameter is the number of decimal places to be retained and the next one (for example, if you enter 2, then the third one is the next one) is rounded, when he When it is a negative number, start from the last digit of the data source and count forward the corresponding length to 0 and round the last digit. For example, round(123456,-2) is 123456 starting from 6 and counting forward two digits become zero, and The last digit of 5 (the first digit from back to front is 6 and the last digit is 5) is rounded off, and the output is 123500

Related recommendations:

The usage and difference between intval() and (int) conversion under PHP

The above is the detailed content of PHP rounding, rounding, use of round function. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!