Code of the rounding function in php (floor function, ceil function, round and intval)

Source: Internet
Author: User
Tags floor function
When processing floating point numbers in php, it is often necessary to rounding them. In php, there are two functions that apply to this situation: The floor function, the ceil function, and the round function floor function and the ceil function work together to make the data processed by php more authentic and reliable.

I. First, let's look at the floor function:

Syntax:

Float floor (float value)

Note:

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.

Floor () Example 1

<?php  echo floor(1.6); // will output "1"  echo floor(-1.6); // will 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

II. ceil functions:

Syntax:

Float ceil (float value)

Note:

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.

Ceil () example:

<?php echo ceil(4.3);  // 5 echo ceil(9.999); // 10 echo ceil(-3.14); // -3 ?>

See the differences between the two functions ..

We usually use
// Page number calculation:

$ Lastpg = ceil ($ totle/$ displaypg); // The last page is the total number of pages. it is much easier to use ceil. $ 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;

Of course, if you need to set the accuracy, you need to use the round function.

III. round functions:

Syntax:

Float round (float val [, int precision])

Note:

Returns the result of rounding val by the specified precision (number of digits after decimal point. Precision can also be negative or zero (default ).

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

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

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

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.