Rounding function code in PHP (floor function, ceil function, round and intval)

Source: Internet
Author: User
Tags floor function
This article mainly introduces the round function code in PHP (floor function, ceil function, round and intval), has a certain reference value, now share to everyone, the need for friends can refer to

When working with floating-point numbers in PHP, you often need to round up. There are two functions in PHP that apply to this scenario: floor function, ceil function, and round function

The floor function and the Ceil function match each other to make the data processed by PHP more realistic and reliable.

First, look at the floor function:

Grammar:

Float floor (float value)

Description

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.

Floor () Example 1


<?php  Echo Floor (1.6);//would output "1"  echo floor ( -1.6);//would output "-2"?>


Floor () Example 2


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


Output:
0
0
5
5
-6
-6

Second, ceil function:

Grammar:

float ceil (float value)

Description

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.

Ceil () Example:


<?php echo ceil (4.3);  5 echo ceil (9.999); Ten echo Ceil (-3.14); -3?>


See the difference between the two functions?

We'll use them when we're paging.
Page Count:


  $LASTPG =ceil ($totle/$DISPLAYPG); The last page, and the total number of pages, is much more convenient with ceil.  $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;


Of course, if you need to make precision you need to use the round function.

Three, round function:

Grammar:

Float round (float val [, int precision])

Description

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

Round () example


<?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?>


Iv. intval-of variables into integer patterns

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


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.