问题
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