Full function rounding in php

Source: Internet
Author: User

Ceil-integer in one 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

The code is as follows: Copy code
<? Php
Echo ceil (4.3); // 5
Echo ceil (9.999); // 10
?>

We usually use


// Page number calculation:

The code is as follows: Copy code
$ Lastpg = ceil ($ totle/$ displaypg); // The last page, also the total number of pages
$ 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;

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:

The code is as follows: Copy code

<? Php
Echo (floor (0.60 ));
Echo (floor (0.40 ));
Echo (floor (5 ));
Echo (floor (5.1 ));
Echo (floor (-5.1 ));
Echo (floor (-1, 5.9 ))
?> Output:

0
0
5
5
-6
-6

Round-rounding floating point numbers
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

The code is as follows: Copy code

<? Php
Echo (roround (0.60 ));
Echo (roround (0.50 ));
Echo (roround (0.49 ));
Echo (round (-4.40 ));
Echo (round (-4.60 ));
?> Output:

1
1
0
-4
-5

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

The code is as follows: Copy code
<? 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.