Round rounding function in PHP in detail

Source: Internet
Author: User
In this article, we introduce The four commonly used rounding and rounding functions in PHP are Ceil,floor,round,intval,Let's take a detailed introduction.

1.ceil: Into a method to take the whole
Description
float ceil (float value)
Returns the next integer not less than value, in which value is entered if there is a fractional part. The type returned by Ceil () is still float, because the range of float values is usually larger than the integer.
Example 1. Ceil () example

<?php echo ceil (4.3); 5 echo ceil (9.999); Ten?>

We'll use them when we're paging.

Page Count:

  $LASTPG =ceil ($totle/$DISPLAYPG); Last page, also the total number of pages     $LASTPG = $lastpg? $LASTPG: 1;//No entries are displayed, the last page is 1     $page =min ($LASTPG, $page);     $PREPG = $page-1; Prev     $NEXTPG = ($page = = $lastpg? 0: $page + 1);//Next     $firstcount = ($page-1) * $DISPLAYPG;

2.floor: Rounding off the whole process
Description
Float floor (float value)
Returns the next integer not less than value, rounding out the fractional part of value. The type returned by floor () is still float, because the range of float values is usually larger than the integer.
Example 1. Floor () example

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

<?phpecho (Floor (0.60)), Echo (Floor (0.40)), Echo (Floor (5)), Echo (Floor (5.1)), Echo (Floor ( -5.1)), Echo (Floor (-5.9) )?>

Output:

0055-6-6

3.round: Rounding a floating-point number
Description
Float round (float val [, int precision])
Returns the result of rounding Val based on the specified precision precision (number of digits after decimal point). Precision can also be negative or 0 (the default value).

Note: PHP does not correctly handle strings that resemble "12,300.2" by default.

Note: The Prec parameter was introduced in PHP 4.
Example

<?phpecho (Round (0.60)), Echo (Round (0.50)), Echo (round (0.49)), Echo (Round ( -4.40)), Echo (Round (-4.60)); >

Output:

110-4-5

4.intval: Turn the variable into an integer form

The variable is converted to an integer type.

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

return value: Integer

Function type: PHP system function

Content Description

This function converts a variable into an integer type. The parameter base that can be omitted is the base of the conversion, and the default value is 10. The converted variable var can be an array or any type variable other than the class.

Example Intval ()

<?php echo intval (4.3); 4 echo intval (4.6); 4?>

Note: intval If the character type is automatically converted to 0 as

Intval (' abc ');

Output results

0

If it is

Intval (' 5fd ');

The output is

5

http://www.bkjia.com/PHPjc/629104.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/629104. Htmltecharticle in this article we introduce the four commonly used rounding and rounding functions in PHP ceil,floor,round,intval, which we will describe in detail below. Ceil into a method to take the whole description of the float ceil (f ...

What is the difference between the Ceil function in 5.php and the Intval function?

Ceil, Intval gets the integer value of the variable

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

Related articles:

Rounding of PHP data processing

3 Ways to rounding PHP

Summary of PHP rounding intercept floating-point string method

  • 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.