Code of the rounding function in php (floor function, ceil function, round and intval), ceilintval_PHP tutorial-php Tutorial

Source: Internet
Author: User
Tags floor function
Code of the rounding function in php (floor function, ceil function, round and intval), ceilintval. The rounding function code (floor function, ceil function, round and intval) in php can be used together with ceilintvalfloor function and ceil function to make the data processed by php more authentic and reliable. Code of the rounding function in php (floor function, ceil function, round and intval), ceilintval

The combination of the floor function and the ceil function makes 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


In php, what is the difference between the ceil function and the intval function?

Float ceil (float value) ceil returns the smallest integer not less than value. the returned value is still float int intval (mixed value [, int base]) intval used to obtain the integer of the variable, returns the integer value of the variable through hexadecimal conversion (decimal by default ). Echo intval (42); // 42
Echo intval (4.2); // 4
Echo intval ('42'); // 42
Echo intval ('+ 42'); // 42
Echo intval ('-42'); //-42
Echo intval (042); // 34
Echo intval ('042'); // 42
Echo intval (1e10); /// 1410065408
Echo intval ('1e10'); // 1
Echo intval (0x1A); // 26
Echo intval (42000000); // 42000000.
Echo intval (42, 8); // 42
Echo intval ('42', 8); // 34
?>
 

In php, what is the difference between the ceil function and the intval function?

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;
 

Combine (floor function, ceil function, round and intval), ceilintval floor function and ceil function work together to make php processing data more authentic and reliable ....

Related Article

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.