Code in PHP for the ceiling function

与世无争的帅哥 提交于 2021-02-19 07:43:32

问题


Anyone has ever programmed a PHP (or Perl) function to get the ceiling value Excel style?


回答1:


This should be the answer, from php.net comments:

// MS Excel function: Ceiling( number, significance ) 


// duplicates m$ excel's ceiling function
if( !function_exists('ceiling') )
{
    function ceiling($number, $significance = 1)
    {
        return ( is_numeric($number) && is_numeric($significance) ) ? (ceil($number/$significance)*$significance) : false;
    }
}

echo ceiling(0, 1000);     // 0
echo ceiling(1, 1);        // 1000
echo ceiling(1001, 1000);  // 2000
echo ceiling(1.27, 0.05);  // 1.30



回答2:


"Microsoft Excel's ceiling function does not follow the mathematical definition, but rather as with (int) operator in C, it is a mixture of the floor and ceiling function: for x ≥ 0 it returns ceiling(x), and for x < 0 it returns floor(x). This has followed through to the Office Open XML file format. For example, CEILING(-4.5) returns -5. A mathematical ceiling function can be emulated in Excel by using the formula "-INT(-value)" (please note that this is not a general rule, as it depends on Excel's INT function, which behaves differently that most programming languages)." - from wikipedia

If php's built in ceil function isn't working right you could make a new function like

function excel_ceil($num){
    return ($num>0)?ceil($num):floor($num);
}

Hope that helps




回答3:


Sorry, not quite clear what 'Excel style' is, but PHP has a ceil function.



来源:https://stackoverflow.com/questions/88831/code-in-php-for-the-ceiling-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!