简体   繁体   English

如何在PHP中舍入/ ceil / floor一个bcmath数字?

[英]How to round/ceil/floor a bcmath number in PHP?

Is there any library function for this purpose, so I don't do it by hand and risk ending in TDWTF? 是否有任何库函数用于此目的,所以我不手动完成并冒险以TDWTF结束?

echo ceil(31497230840470473074370324734723042.6);

// Expected result
31497230840470473074370324734723043

// Prints
<garbage>

UPDATE: See my improved answer here: How to ceil, floor and round bcmath numbers? 更新:在这里看到我改进的答案: 如何cel,floor和round bcmath数字? .


These functions seem to make more sense, at least to me: 这些功能似乎更有意义,至少对我来说:

function bcceil($number)
{
    if ($number[0] != '-')
    {
        return bcadd($number, 1, 0);
    }

    return bcsub($number, 0, 0);
}

function bcfloor($number)
{
    if ($number[0] != '-')
    {
        return bcadd($number, 0, 0);
    }

    return bcsub($number, 1, 0);
}

function bcround($number, $precision = 0)
{
    if ($number[0] != '-')
    {
        return bcadd($number, '0.' . str_repeat('0', $precision) . '5', $precision);
    }

    return bcsub($number, '0.' . str_repeat('0', $precision) . '5', $precision);
}

They support negative numbers and the precision argument for the bcround() function. 它们支持负数和bcround()函数的精度参数。

Some tests: 一些测试:

assert(bcceil('4.3') == ceil('4.3')); // true
assert(bcceil('9.999') == ceil('9.999')); // true
assert(bcceil('-3.14') == ceil('-3.14')); // true

assert(bcfloor('4.3') == floor('4.3')); // true
assert(bcfloor('9.999') == floor('9.999')); // true
assert(bcfloor('-3.14') == floor('-3.14')); // true

assert(bcround('3.4', 0) == number_format('3.4', 0)); // true
assert(bcround('3.5', 0) == number_format('3.5', 0)); // true
assert(bcround('3.6', 0) == number_format('3.6', 0)); // true
assert(bcround('1.95583', 2) == number_format('1.95583', 2)); // true
assert(bcround('5.045', 2) == number_format('5.045', 2)); // true
assert(bcround('5.055', 2) == number_format('5.055', 2)); // true
assert(bcround('9.999', 2) == number_format('9.999', 2)); // true

This will work for you: 这对你有用:

$x = '31497230840470473074370324734723042.9';

bcscale(100);
var_dump(bcFloor($x));
var_dump(bcCeil($x));
var_dump(bcRound($x));

function bcFloor($x)
{
    $result = bcmul($x, '1', 0);
    if ((bccomp($result, '0', 0) == -1) && bccomp($x, $result, 1))
        $result = bcsub($result, 1, 0);

    return $result;
}

function bcCeil($x)
{
    $floor = bcFloor($x);
    return bcadd($floor, ceil(bcsub($x, $floor)), 0);
}

function bcRound($x)
{
    $floor = bcFloor($x);
    return bcadd($floor, round(bcsub($x, $floor)), 0);
}

Basically it finds the flooy by multiplying by one with zero precision. 基本上它通过乘以零精度来找到浮标。

Then it can do ceil / round by subtracting that from the total, calling the built in functions, then adding the result back on 然后它可以通过从总计中减去,调用内置函数,然后重新添加结果来执行ceil / round

Edit: fixed for -ve numbers 编辑:固定为-ve数字

OK, for my high-precision Money library , which is currently on hundreds of production sites, I had to completely rewrite this bcround functionality. 好的,对于我目前在数百个生产网站上的高精度Money库 ,我必须完全重写这个bcround功能。 Nothing I found on the entire Internet was up to code. 我在整个互联网上找到的任何东西都取决于代码。

Here's what I came up with: 这是我想出的:

/**
 * Based off of https://stackoverflow.com/a/1653826/430062
 * Thanks, [Alix Axel](https://stackoverflow.com/users/89771/alix-axel)!
 *
 * @param $number
 * @param int $precision
 * @return string
 */
function bcround($number, $precision = BCMathCalcStrategy::PRECISION)
{
    if (strpos($number, '.') !== false) {
        if ($number[0] != '-') return bcadd($number, '0.' . str_repeat('0', $precision) . '5', $precision);
        return bcsub($number, '0.' . str_repeat('0', $precision) . '5', $precision);
    }

    // Pad it out to the desired precision.
    return number_format($number, $precision);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM