PHP:意外的变量比较结果。 可能的舍入问题[重复]

This question already has an answer here:

I’m working with multiple payment systems some of which process payments with no decimal and others which use the decimal point. My prices are stored with no decimal place and I make conversions when necessary. I have a sliding scale for payments which are defined in the code below; e.g. 1 year can be purchased for $12.99 or 5 years for $29.99. I’m getting errors with my code when a user selects the two year option.

Below is some example code which illustrates the problem I’m having on my site. Whenever a user selects the two year pricing the $years variable is not defined and an error is thrown. I do not know why all of the other defined year values evaluate to true when tested, but the 1999 number does not. Any thoughts on this issue are much appreciated. The output of the example code below is as follows:

1, Error, 3, 4, 5,

<?php

define('year1', 1299);
define('year2', 1999);
define('year3', 2499);
define('year4', 2799);
define('year5', 2999);

$prices = array_values(array('YearOne' => 12.99, 'YearTwo' => 19.99, 'YearThree' => 24.99, 'YearFour' => 27.99, 'YearFive' => 29.99));

for ($x = 0; $x < sizeof($prices); $x++) {
$proPrice = $prices[$x] * 100;

switch ($proPrice) {
    case year1:
        $years = 1;
        break;
    case year2:
        $years = 2;
        break;
    case year3:
        $years = 3;
        break;
    case year4:
        $years = 4;
        break;
    case year5:
        $years = 5;
        break;
    default:
        $years = 'Error';
}

echo $years . ', ';
}
?>

If I change $proPrice = $prices[$x] * 100; to $proPrice = round($prices[$x] * 100,0); I no longer have the unexpected results, but I would like to know the reason why using the round function is necessary for the 1999 value, but none of the others.

</div>

This issue is due to floating point numbers, see the big Warning in PHP Mannual.

You may use bcmul, which results as expected, see example below:

define('year1', 1299);
define('year2', 1999);
define('year3', 2499);
define('year4', 2799);
define('year5', 2999);

$prices = array_values(array('YearOne' => 12.99, 'YearTwo' => 19.99, 'YearThree' => 24.99, 'YearFour' => 27.99, 'YearFive' => 29.99));
$length = sizeof($prices); //taking it out from loop comparison, as there is no need to get the size of array, which isn't changing.

for ($x = 0; $x < $length; $x++) {
$proPrice = bcmul($prices[$x],100);

switch ($proPrice) {
    case year1:
        $years = 1;
        break;
    case year2:
        $years = 2;
        break;
    case year3:
        $years = 3;
        break;
    case year4:
        $years = 4;
        break;
    case year5:
        $years = 5;
        break;
    default:
        $years = 'Error';
}

echo $years . ', ';
}