PHP Rounding Precision decimal places and rounding _php skills

Source: Internet
Author: User
Tags sprintf

An integer method for rounding, rounding, ignoring decimals, etc. in one method

PHP to take the integer function commonly used four methods, the following collection of four functions, often used to take the whole function, today a small sum! Actually very simple, is just a few functions ~ ~ mainly: Ceil,floor,round,intval
PHP takes an integer function commonly used four methods, the following collection of four functions;
Often used to take the whole function, today a small sum up! Actually very simple, is just a few functions ~ ~ mainly: Ceil,floor,round,intval

First, ceil-into one method to take the whole

Description
float ceil (float value)
Returns the next integer that is not less than value, or the value if there is a decimal part. The type returned by Ceil () is still float, because the float value is usually larger than the integer.
Example 1. Ceil () example

Copy Code code as follows:

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

Second, the floor-the method of rounding

Description
Float floor (float value)
Returns the next integer not more than value, rounding out the decimal part of value. The type returned by floor () is still float, because the float value is usually larger than the integer.
Example 1. Floor () example

Copy Code code as follows:

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

Third, round-to rounding floating point numbers

Description
Float round (float val [, int precision])
Returns the result of rounding Val according to the specified precision precision (number of digits after the decimal point). Precision can also be negative or 0 (the default value).
Example 1. Round () example

Copy Code code as follows:

<?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
?>

intval-the variable into an integer form

Example Intval ()

Copy Code code as follows:

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

PHP Rounding Accurate decimal places and rounding

(1) PHP retains three decimal places and rounded

Copy Code code as follows:

$num = 0.0215489;
Echo sprintf ("%.3f", $num); 0.022

(2) PHP keep three decimal digits not rounded

Copy Code code as follows:

$num = 0.0215489;
Echo substr (sprintf ("%.4f", $num), 0,-1); 0.021

(3) php into a method to take an integer (this in the paging program in the number of pages in the program will be used)

Copy Code code as follows:

echo ceil (4.3); 5
echo ceil (9.999); 10

(4) The method of the PHP house to take the integer

Copy Code code as follows:

echo floor (4.3); 4
echo Floor (9.999); 9

(5), round function

Example 1. Round () example

Copy Code code as follows:

<?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
?>

PHP Rounding is the most accurate way to keep a two-digit decimal point

Copy Code code as follows:

<?php
$number = 123213.066666;
Echo sprintf ("%.2f", $number);
?>

Output results:
123213.07

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.