Example tutorial for PHP decimal rounding

Source: Internet
Author: User
Tags php programming
The content of this section:
PHP, JS small number Rounding Method

The first part:

1, discard the fractional part, and keep the whole number of parts

Copy Code code example:

Php:intval (7/2) js:parseint (7/2)

2, rounding up, with decimals on the integer part plus 1

Copy Code code example:

Php:ceil (7/2) Js:Math.ceil (7/2)

3, rounded.

Copy Code code example:

Php:round (7/2) js:Math.round (7/2)

4, rounding down

Copy Code code example:

Php:floor (7/2) Js:Math.floor (7/2)

The second part, PHP takes the whole common function
In PHP programming, the function of rounding is often used ceil, floor, round, intval.

Let's introduce them separately.

1,ceil--in-process rounding

Introduction description
float ceil (float value)

Returns not less than =noted id=note_temp>value the next integer, value if there is a fractional part to carry, ceil () return type is still float,float value range is usually larger than Eger.

Example 1. Ceil

Copy Code code example:

<?phpecho ceil (4.3);     5echo ceil (9.999);     10?>

2,floor--The rounding-off method

Introduction description

Float floor (float value)

Returns less than value the next integer takes the value fractional part to take the whole floor return type is still float float value range is usually larger than Eger

Example 1. Floor

Copy Code code example:

<?phpecho floor (4.3);     4echo floor (9.999); 9?>

3,round--4 5 in floating point

Introduction description

Float round (float Val [, Precision])

Returns Val Precision (number of digits after decimal decimal point) for 4 5 into the result precision can also be negative or 0 (default)

Example 1. Round

Copy Code code example:

<?phpecho round (3.4); 3echo round (3.5); 4echo round (3.6);//4echo round (3.6, 0);//4//Www.jbxue.comecho round (1.95583, 2); 1.96echo round (1241757,-3); 1242000echo round (5.045, 2); 5.05echo round (5.055, 2); 5.06?>

4,val---to integer patterns of variables
Example 1,val

Copy Code code example:

<?phpecho val (4.3);    4echo Val (4.6); 4  ?>
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.