天花板函数的 PHP 代码

发布于 2024-07-04 11:08:48 字数 46 浏览 3 评论 0原文

有人曾经编写过 PHP(或 Perl)函数来获取 Excel 样式的上限值吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事灯 2024-07-11 11:08:48

这应该是来自 php.net 评论的答案:

// 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

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
行至春深 2024-07-11 11:08:48

“Microsoft Excel 的上限函数不遵循数学定义,而是与 C 中的 (int) 运算符一样,它是下限函数和上限函数的混合体:对于 x ≥ 0,它返回上限(x),而对于x < 0 则返回 Floor(x)。例如,CEILING(-4.5) 返回 -5。可以使用公式“-”来模拟数学上限函数。 INT(-value)”(请注意,这不是一般规则,因为它取决于 Excel 的 INT 函数,该函数的行为与大多数编程语言不同)。” - 来自 wikipedia

如果 php 内置的 ceil 函数不能正常工作,你可以创建一个新函数,比如

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

希望有帮助

"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

末骤雨初歇 2024-07-11 11:08:48

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文