Home > Backend Development > PHP Problem > How to get only the integer part in php

How to get only the integer part in php

藏色散人
Release: 2023-03-10 20:32:02
Original
4993 people have browsed it

php method to only round the integer part: 1. Round directly through intval; 2. Round up through round; 3. Round up through ceil; 4. Round down through floor.

How to get only the integer part in php

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

How does php only take the integer part?

PHP rounding, rounding up, rounding down, decimal interception

Four commonly used methods of PHP rounding function:

1.Direct Round, discard decimals, keep integers: intval();

2. Round to integers: round();

3. Round up, add 1 if there are decimals: ceil() ;

4. Round down: floor().

1. intval—convert the variable into an integer type

If intval is a character type, it will be automatically converted to 0.

intval(3.14159);  // 3
intval(3.64159);  // 3
intval('ruesin'); //0
Copy after login

2. Rounding: round()

Round parameter 1 according to the precision specified by parameter 2. Parameter 2 can be negative or zero (default).

round(3.14159);      // 3
round(3.64159);      // 4
round(3.64159, 0);   // 4
round(3.64159, 2);   // 3.64
round(5.64159, 3);   // 3.642
round(364159, -2);   // 364200
Copy after login

3. Round up and add 1 if there is a decimal: ceil()

Returns the next integer that is not less than value. If value has a decimal part, it will be rounded up by one.

This method is often used when we write paging classes to calculate the number of pages.

ceil(3.14159);  // 4
ceil(3.64159);  // 4
Copy after login

4. Round down: floor()

Returns the next integer that is not greater than value, and rounds the decimal part of value.

floor(3.14159);    // 3
floor(3.64159);    // 3
Copy after login

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to get only the integer part in php. 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