-5

I have these variables:

$roundoff1 = 1888.8861;

$roundoff2 = 1888.8851;

$roundoff3 = 1888.8841;

$roundoff4 = 1888.8881;

I want these outputs:

$roundoff1 output = 1,888.88

$roundoff2 output = 1,888.89

$roundoff3 output = 1,888.88

$roundoff1 output = 1,888.88

Second digit from right decides the rounding.
Even digit, third digit from right stays the way it is.
Odd digit, third digit from right is rounded up.

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();
Andreas
  • 23,610
  • 6
  • 30
  • 62

2 Answers2

1

Duplicate

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

Use number_format():

First remove last digit of every number.

$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

Then make two digit decimal number as follows :

$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;

Output

1888.88
1888.89
1888.88
1888.88
GYaN
  • 2,327
  • 4
  • 19
  • 39
  • hmm thanks but I need this one. I want this output: $roundoff1 = 1888.8861; $roundoff2 = 1888.8851; $roundoff3 = 1888.8841; $roundoff4 = 1888.8881; '1,888.88 1,888.89 1,888.88 1,888.88' – Mia Torres Nov 25 '17 at 08:37
  • This was your words i just want to round off it to 2 decimal point but I have problem when using abs(); – Mia Torres 21 mins ago – GYaN Nov 25 '17 at 08:39
  • how about rounding off them using even numbers? – Mia Torres Nov 25 '17 at 08:42
  • Okay.. You want that if your second last number is even then output even else odd... Right? – GYaN Nov 25 '17 at 08:44
  • yeah I already try abs(); /(2, PHP_ROUND_HALF_EVEN) / round(); not working. got wrong output. (2, PHP_ROUND_HALF_EVEN) working when the number is LOWER(2 and 4). – Mia Torres Nov 25 '17 at 08:49
  • I'll try it Thanks mr. @gyandeep Sharma :D – Mia Torres Nov 25 '17 at 09:10
1

I split the string and use modulo calculation to find out if it's even or odd digit.
Then I use implode and array slice to get the correct number of digits and create a string as return.

$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);

Output:

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

https://3v4l.org/2ochB

Neodan
  • 5,154
  • 2
  • 27
  • 38
Andreas
  • 23,610
  • 6
  • 30
  • 62