我应该用什么来取消偶数?

时间:2017-11-25 08:11:19

标签: php codeigniter-3

我有这些变量:

$roundoff1 = 1888.8861;

$roundoff2 = 1888.8851;

$roundoff3 = 1888.8841;

$roundoff4 = 1888.8881;

我想要这些输出:

$roundoff1 output = 1,888.88

$roundoff2 output = 1,888.89

$roundoff3 output = 1,888.88

$roundoff1 output = 1,888.88

右起第二位数决定四舍五入 即使是数字,右边的第三位也保持原样 奇数位,右边第三位数字向上舍入。

if ($waybill_data['discount_amount']  != 0 ) {

        $sales_discount = abs($rec->amount * .1);
        $holding_tax    = abs($rec->amount - $sales_discount) / 1.12 * .02;  
        $cib            = abs($rec->amount - $sales_discount - $holding_tax);

        /* CASH IN BANK CLEARING */
        // $j->amount    = round($cib , 2, PHP_ROUND_HALF_EVEN); 
        $j->amount    = abs($cib); 
        $j->account   = 13006;
        $j->type      = 'DR';

        $j->onpost = 1;

        $j->description  = 'Payment("'.$waybill_data['customer_type'].'") for Waybill # '.$j->waybillno;
        $j->save_on_history1();

        //----------------------------------------------------------------------------------------------

        /* SALES DISCOUNT */
        // $j->amount    = round($sales_discount , 2, PHP_ROUND_HALF_EVEN); 
        $j->amount    = abs($sales_discount); 
        $j->account   = 32001;
        $j->type      = 'DR';

        $j->onpost = 1;

        $j->description  = 'Payment("'.$waybill_data['customer_type'].'") for Waybill # '.$j->waybillno;
        $j->save_on_history1();

        //----------------------------------------------------------------------------------------------

        /* WITH HOLDING TAX */
        // $j->amount    = round($holding_tax , 2, PHP_ROUND_HALF_EVEN); 
        $j->amount    = abs($holding_tax);
        $j->account   =  21005;
        $j->type      = 'DR';

        $j->onpost = 1;

        $j->description  = 'Payment("'.$waybill_data['customer_type'].'") for Waybill # '.$j->waybillno;
        $j->save_on_history1();

        //----------------------------------------------------------------------------------------------

        /* COLLECT */
        $j->amount    = $rec->amount;  
        $j->account   = $account_type;
        $j->type      = 'CR';

        $j->onpost = 1;

        $j->description  = 'Payment("'.$waybill_data['customer_type'].'") for Waybill # '.$j->waybillno;
        $j->save_on_history1();

2 个答案:

答案 0 :(得分:1)

Duplicate

$roundoff1 = 1888.8861;
$roundoff2 = 1888.8851;
$roundoff3 = 1888.8841;
$roundoff4 = 1888.8881;

使用number_format():

首先删除每个数字的最后一位数字。

$newr1 = substr($roundoff1, -2,1);    //1888.886
$newr2 = substr($roundoff2, -2,1);    //1888.885
$newr3 = substr($roundoff3, -2,1);    //1888.884
$newr4 = substr($roundoff4, -2,1);    //1888.888

然后按如下所示输入两位十进制数:

$rr1 = number_format((float)$roundoff1, 2, '.', '');    //1888.89
$rr2 = number_format((float)$roundoff2, 2, '.', '');    //1888.89
$rr3 = number_format((float)$roundoff3, 2, '.', '');    //1888.88
$rr4 = number_format((float)$roundoff4, 2, '.', '');    //1888.89


if((($newr1%2)==0)&&($newr1>5)) $rr1 = ($rr1-0.01);  //Check for even number greater then 5 and subtract with 0.01
elseif((($newr1%2)==1)&&($newr1<5)) $rr1 = ($rr1+0.01);  //Check for odd number less then 5 and add 0.01

if((($newr2%2)==0)&&($newr2>5)) $rr2 = ($rr2-0.01); //Check for even number greater then 5 and subtract with 0.01
elseif((($newr2%2)==1)&&($newr2<5)) $rr2 = ($rr2+0.01); //Check for odd number less then 5 and add 0.01

if((($newr3%2)==0)&&($newr3>5)) $rr3 = ($rr3-0.01); //Check for even number greater then 5 and subtract with 0.01
elseif((($newr3%2)==1)&&($newr3<5)) $rr3 = ($rr3+0.01);  //Check for odd number less then 5 and add 0.01

if((($newr4%2)==0)&&($newr4>5)) $rr4 = ($rr4-0.01);  //Check for even number greater then 5 and subtract with 0.01
elseif((($newr4%2)==1)&&($newr4<5)) $rr4 = ($rr4+0.01);  //Check for odd number less then 5 and add 0.01

echo $rr1.'<br>'.$rr2.'<br>'.$rr3.'<br>'.$rr4;

<强>输出

1888.88
1888.89
1888.88
1888.88

答案 1 :(得分:1)

我拆分字符串并使用模数计算来确定它是偶数还是奇数 然后我使用implode和数组切片来获得正确的数字位数并创建一个字符串作为返回。

$arr = [1888.8861, 1888.8851, 1888.8841, 1888.8881];

foreach ($arr as &$num) {
    $parts = str_split($num); // split string to parts
    if ($parts[count($parts) - 2] % 2 == 0) {
        // Even, implode parts to "round down"
        $num = implode("", array_slice($parts, 0, count($parts) - 2));
    } else {
        // Odd, implode and add one to the the digit that should round up
        $num = implode("", array_slice($parts, 0, count($parts) - 3)) . ($parts[count($parts) - 3] + 1);
    }
}
var_dump($arr);

输出:

array(4) {
    [0]=>"1888.88"
    [1]=>"1888.89"
    [2]=>"1888.88"
    [3]=>"1888.88"
}

https://3v4l.org/2ochB