doulubashang6936 2015-06-30 04:28
浏览 42

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>
  • 写回答

1条回答 默认 最新

  • dongzhuoxie1244 2015-06-30 06:20
    关注

    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 . ', ';
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错