3

My payment system takes 0.5% from each transaction. I have 0.9$ on my account, very small amount. And when I try to withdraw 0.9$ through API it gives an error, it takes 0.0045$ as fee and rounds to 0.91$. So I can only withdraw 0.89$ and pay 0.00445$ fee, which is rounded to 1 cent to empty my account;

I can't understand by which formula the additional fee is rounded =(

php round(0.9+0.9*0.005,2) 

returns 0.9$, how to get 0.91$?

Edit: ceil works good for this particular amount, but on practice when I withdraw only 1 cent the comission, which is 0.00005 somehow does not count as 0.01, looks like besides ceil() only first two zeros after point are included. hm..

ceil((0.01*0.005) * 100) / 100; gives 1 cent, but in reality api takes zero.

user1482261
  • 191
  • 3
  • 15

2 Answers2

6

How about using ceil

$number = 0.9*0.005;
$number = ceil(100*$number) / 100; // rounds fee up to nearest cent
$number = $number + 0.9;
echo round($number,2);
jh314
  • 27,144
  • 16
  • 62
  • 82
0

Do not do multiplication inside a ceil function! You'll get floating point errors and it can be extremely unpredictable. To avoid this do:

function ceiling($value, $precision = 0) {
    $offset = 0.5;
    if ($precision !== 0)
        $offset /= pow(10, $precision);
    return round($value + $offset, $precision, PHP_ROUND_HALF_DOWN);
}

For example ceiling(2.2200001, 2) will give 2.23.

Nico Westerdale
  • 2,147
  • 1
  • 24
  • 31