Full function rounding in php

Source: Internet
Author: User
In this article, we will introduce four common functions in php: ceil, floor, round, and intval. Ceil-floatceil (floatvalue)

This article introduces four common functions in php: ceil, floor, round, and intval.

Ceil-integer in one method

Description

Float ceil (float value)

Returns the next integer not less than the value. if the value has a decimal part, the type returned by one. ceil () is still float, because the float value range is usually larger than integer.

Example 1. ceil () example

The instance code is as follows:

  1. Echo ceil (4.3); // 5
  2. Echo ceil (9.999); // 10
  3. ?>

We usually use

// Page number calculation:

The instance code is as follows:

  1. $ Lastpg = ceil ($ totle/$ displaypg); // The last page, also the total number of pages
  2. $ Lastpg = $ lastpg? $ Lastpg: 1; // no entries are displayed. set the last page to 1.
  3. $ Page = min ($ lastpg, $ page );
  4. $ Prepg = $ page-1; // Previous page
  5. $ Nextpg = ($ page = $ lastpg? 0: $ page + 1); // Next page
  6. $ Firstcount = ($ page-1) * $ displaypg;

Floor-rounding

Description

Float floor (float value)

Returns the next integer not greater than the value. rounds the fractional part of the value to an integer. floor () returns the float type, because the float value range is usually larger than integer.

Example 1. floor () example

Example

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

The instance code is as follows:

  1. Echo (floor (0.60 ));
  2. Echo (floor (0.40 ));
  3. Echo (floor (5 ));
  4. Echo (floor (5.1 ));
  5. Echo (floor (-5.1 ));
  6. Echo (floor (-1, 5.9 ))
  7. ?>

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). The precision can also be a negative number 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 ..

The instance code is as follows:

  1. Echo (roround (0.60 ));
  2. Echo (roround (0.50 ));
  3. Echo (roround (0.49 ));
  4. Echo (round (-4.40 ));
  5. Echo (round (-4.60 ));
  6. ?>

Output:

1

1

0

-4

-5

Intval-convert a variable to an integer type

Variable to integer type.

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

Return value: integer

Function types: PHP system functions

Description

This function can convert a variable to an integer type. the base parameter that can be omitted is the basis of the conversion. the default value is 10. the transformed variable var can be any type variable other than the array or class.

Example intval ()

The instance code is as follows:

  1. Echo intval (4.3); // 4
  2. Echo intval (4.6); // 4
  3. ?>

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

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.