Home > Backend Development > PHP Problem > What should I do if php cannot be removed?

What should I do if php cannot be removed?

藏色散人
Release: 2023-03-08 07:48:01
Original
2558 people have browsed it

php solutions to endless divisions: 1. Use the round function to round floating point numbers; 2. Use the ceil method to achieve rounding; 3. Use the floor function to achieve rounding. .

What should I do if php cannot be removed?

#The operating environment of this article: Windows7 system, PHP7.1, Dell G3 computer.

php Division rounding

If we use the "/" operator to perform division operation, if we encounter a situation that cannot be divided, we will get a decimal value. What if I only want the integer part?

1.round — Rounds a floating-point number

float round ( float $val [, int $precision ] )
Copy after login

Returns val rounded to the specified precision (the number of decimal digits after the decimal point). precision can also be negative or zero (default).

//Example #1 round() 例子
<?php
echo round(3.4); // 3
echo round(3.5); // 4
echo round(3.6); // 4
echo round(3.6, 0); // 4
echo round(1.95583, 2); // 1.96
echo round(1241757, -3); // 1242000
echo round(5.045, 2); // 5.05
echo round(5.055, 2); // 5.06
?>
Copy after login

Note: PHP cannot handle strings like "12,300.2" correctly by default. See Convert String to Numeric.

Recommended: "PHP Video Tutorial"

2.ceil — Further rounding (rounding up)

float ceil ( float $value )
Copy after login

Returns no less than value The next integer of value, if value has a decimal part, add one digit. The type returned by ceil() is still float, because the range of float values ​​is usually larger than that of integer.

//Example #1 ceil() 例子
<?php
echo ceil(4.3); // 5
echo ceil(9.999); // 10
?>
Copy after login

3.floor — Rounding by rounding method (rounding down)

float floor ( float $value )
Copy after login

Returns the next integer that is not greater than value, and rounds the decimal part of value. The type returned by floor() is still float, because the range of float values ​​is usually larger than that of integer.

//Example #1 floor() 例子
<?php
echo floor(4.3); // 4
echo floor(9.999); // 9
?>
Copy after login

The above is the detailed content of What should I do if php cannot be removed?. 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