Same number different formats in PHP
I have two numbers in PHP. 81.0000 and 81. While they are equal in reality I cannot get them to be equal in PHP.
I have tried casting both numbers to float and they still won't come thought开发者_StackOverflow中文版 as equal.
Anyone have any idea how I can have these two numbers be the same?
Check out the awesome WARNING on php.net:
never trust floating number results to the last digit, and never compare floating point numbers for equality.
The very best you can to is type cast to (int)
, or use PHP rounding functions like round()
, floor()
, or ceil()
.
UPDATE
Check out the Arbitrary Precision Math Functions such as the one @Jose Vega pointed out in his answer. They should get you where you need to go.
bccomp — Compare two arbitrary precision numbers
bccomp ( string $left_operand , string $right_operand [, int $scale ] )
<?php
echo bccomp('1', '2') . "\n"; // -1
echo bccomp('1.00001', '1', 3); // 0
echo bccomp('1.00001', '1', 5); // 1
?>
精彩评论