PHP floating point usage and problem Summary _php tutorial

Source: Internet
Author: User
There are a lot of PHP floating point processing functions in PHP, including like Ceil,floor,round,intval after they have been processed to return the number of floating-point numbers we want, let me introduce some of the usage of PHP floating point and problem analysis.

One, PHP floating point usage

PHP floating-point type rounding ceil-into a method of rounding
Description
float ceil (float value)
Returns the next integer not less than value, in which value is entered if there is a fractional part. The type returned by Ceil () is still float, because the range of float values is usually larger than the integer.

Example 1. Ceil () example

The code is as follows Copy Code

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

PHP floor-Rounding of floating-point type rounding Method

Description
Float floor (float value)
Returns the next integer not less than value, rounding out the fractional part of value. The type returned by floor () is still float, because the range of float values is usually larger than the integer.

Example 1. Floor () example

The code is as follows Copy Code

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

PHP floating-point type rounding round-floating-point numbers
Description
Float round (float val [, int precision])
Returns the result of rounding Val based on the specified precision precision (number of digits after decimal point). Precision can also be negative or 0 (the default value).

Example 1. Round () example

The code is as follows Copy Code


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

The intval-of PHP floating-point type rounding the variable into integer form
Example Intval ()

The code is as follows Copy Code
< PHP
echo intval (4.3); 4
Echo intval (4.6); 4
?>

Second, the floating-point number turns into a string

PHP's built-in Echo, Var_dump, Json_encode, string concatenation and other functions (directives) are problematic when displaying floating-point numbers, resulting in loss of precision.

The code is as follows Copy Code

  

$a = 1315537636.338467;

printf ("%f", $a); echo "n";

echo $a. "N";

echo $a; echo "n";

?>

Results

1315537636.338467

1315537636.3385

1315537636.3385

That is, using PHP's most comfortable way to convert a floating point number to a string or display is not possible, you must use printf/sprintf to convert the floating-point number to a string.


Three, the answer to a frequently asked question

However, I missed a point, which is the answer to the following FAQ:

The code is as follows Copy Code

$f = 0.58;
Var_dump (Intval ($f * 100));
?>

Why output 57


Why is the output 57? PHP bug?

I believe that a lot of students have had this kind of doubt, because the light asks me the similar question person to be many, not to mention bugs.php.net often people ask ...

To understand this, first we need to know the representation of floating-point numbers (IEEE 754):

Floating-point numbers, in the case of a 64-bit length (double), take the 1-bit sign bit (E), 11 exponent (Q), and 52-bit mantissa (M) (altogether 64 bits).

Sign bit: The highest bit represents the positive or negative of the data, 0 is a positive number, and 1 indicates a negative number.

Digits: Indicates that the data is a power of 2, and the exponent is represented by an offset code

Mantissa: A valid number that represents the decimal point of the data.

Here is the key point is that the decimal in the binary representation, about how to use binary decimal notation, you can Baidu, I do not repeat here, we key to understand, 0.58 for the binary representation, is an infinitely long value (the following number omitted the implied 1):

The binary representation of 0.58 basically (52 bits) is:
0010100011110101110000101000111101011100001010001111
The binary representation of 0.57 basically (52 bits) is:
0010001111010111000010100011110101110000101000111101

and the binary of both, if only through the 52-bit calculation, respectively:
0.58-0.57999999999999996
0.57-0.56999999999999995


As for the 0.58 * 100 of the specific floating-point multiplication, we do not consider so thin, interesting to see (floating point), we are vague in mental arithmetic to see ... 0.58 * 100 = 57.999999999

Then you intval, Nature is 57 ....

Visible, the key point of this problem is: "You seem to have a poor decimal, in the computer binary representation is infinite"

Four, float (floating point) comparison problem

Recently in the development of a contract management system, involving two floating-point comparison, I am depressed miserable.
N long ago, do not know from where to listen to a "Do not use the equal sign to compare floating point" of the "truth", oneself usually also in use, as if there is no problem, but this problem is finally coming.

The code is as follows Copy Code

$sum = "12300.00";
$a = "10000.30";
$b = "2000.30";
$c = "299.40";

$sum = (float) $sum;
$s = (float) ($a + $b + $c);
Var_dump ($sum, $s);
Var_dump ($sum = = $s);

The result is:

The code is as follows Copy Code

Float (12300)
Float (12300)
BOOL (FALSE)

Later I know in PHP, to compare the size of two floating-point numbers, you can use Bccomp (parameter 1, parameter 2, decimal place) to compare.

The code is as follows Copy Code

$sum = "12300.00";
$a = "10000.30";
$b = "2000.30";
$c = "299.40";

$sum = (float) $sum;
$s = (float) ($a + $b + $c);
Var_dump ($sum, $s);
Var_dump (Bccomp ($sum, $s, 2));

Results:

Float (12300)
Float (12300)
Int (0)//0 means two floating-point values are equal

Bccomp function specific usage participate in PHP manual

http://www.bkjia.com/PHPjc/632704.html www.bkjia.com true http://www.bkjia.com/PHPjc/632704.html techarticle There are a lot of PHP floating point processing functions in PHP, including like Ceil,floor,round,intval after they have been processed to return the number of floating-point numbers we want, let me introduce a ...

  • 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.