1

有没有人编写过 PHP(或 Perl)函数来获得 Excel 风格的上限?

4

3 回答 3

8

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
于 2012-04-10T15:17:49.437 回答
7

"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

于 2008-09-18T00:23:17.063 回答
2

抱歉,不太清楚“Excel 风格”是什么,但 PHP 有一个ceil函数。

于 2008-09-18T00:11:07.823 回答