Full function rounding in php

Source: Internet
Author: User
Php provides a detailed description of rounding the entire function. In this article, we will introduce four common functions in php: ceil, floor, round, and intval. Explain floatceil by using the ceil-first method (f This article introduces Four common functions in php: ceil, floor, round, intval,The following is a detailed introduction.

1. ceil: perform an integer operation in the first method.
Description
Float ceil (float value)
Returns the next integer of no less than value. if the value has a decimal part, it is entered in one place. The type returned by ceil () is still float, because the float value range is usually larger than integer.
Example 1. ceil () example

 

We usually use

// Page number calculation:

$ Lastpg = ceil ($ totle/$ displaypg); // The last page, also the total page number $ lastpg = $ lastpg? $ Lastpg: 1; // no entries are displayed. set the last page to 1 $ page = min ($ lastpg, $ page); $ prepg = $ page-1; // Previous page $ nextpg = ($ page = $ lastpg? 0: $ page + 1); // Next page $ firstcount = ($ page-1) * $ displaypg;

2. floor: Rounding
Description
Float floor (float value)
Returns the next integer not greater than value and rounds the decimal part of value. The return type of floor () is still float, because the float value range is usually larger than that of integer.
Example 1. floor () example

Example
In this example, we will apply the floor () function to different numbers:

 

Output:

0055-6-6

3. round: round the floating point number
Description
Float round (float val [, int precision])
Returns the result of rounding val by the specified precision (number of digits after decimal point. Precision can also be negative or zero (default ).

Note: PHP cannot properly process strings similar to "12,300.2" by default.

Note: The prec parameter is introduced in PHP 4 ..
Example

 

Output:

110-4-5

4. intval: convert a variable to an integer type.

Convert a variable to an integer.

Syntax: int intval (mixed var, int [base]);

Return value: integer

Function types: PHP system functions

Description

This function converts a variable to an integer. The base parameter that can be omitted is the basis for conversion. the default value is 10. The converted variable var can be any type variable outside the array or class.

Example intval ()

 

Note: intval is automatically converted to 0 if it is of the optimized type. for example

intval('abc');

Output result

 0

If yes

intval('5fd');

The output result is

5

Details. Ceil returns float ceil (f...

5. What are the differences between the ceil and intval functions in php?

Ceil returns an integer using the first method, and intval obtains the integer of the variable.

ceil(3.33)=4;intval(3.33)=3;ceil(3.9)=4;intval(3.9)=3;

Related articles:

Rounding php Data processing

Three methods to implement rounding in PHP

Summary of methods for intercepting float strings in php rounding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.