PHP Round function - round up to 2 dp?

前端 未结 5 959
孤街浪徒
孤街浪徒 2020-12-21 02:38

In PHP how would i round up the value 22.04496 so that it becomes 22.05? It seems that round(22.04496,2) = 22.04. Should it not be 22.05??

Thanks in advance

5条回答
  •  我在风中等你
    2020-12-21 03:00

    I think the best way:

    echo ceil(round($value * 100)) / 100;
    

    Example:

    $value = 77.4;
    echo ceil($value * 100) / 100; // 77.41 - WRONG!
    echo ceil(round($value * 100)) / 100; // 77.4 - OK!
    

提交回复
热议问题