Home > Mobile >  Problem with adding taxes to a cents price
Problem with adding taxes to a cents price

Time:03-02

I have the problem that I need 11.90 instead of 11.9 which is given to me. Now the question is how I should do that. if I now as $money a cents value like 1000 hand over it is 10.00€ well but as soon as I add taxes on it are 10.9 need but at something like 10.90.

if (! function_exists('get_tax_amount')) {
function get_tax_amount(Money $money)
{
    return $money->getAmount() * 1.19 / 100;
}
}

i get a "1000" when i dont make * 1.19 / 100; but for tax i need that *1.19 / 100.

What can i do?

CodePudding user response:

Try using round(4.96754,2). This should return you the number with 2 decimal points.

CodePudding user response:

There are a few options, what is happening now is it is returning a float.

One way is to return a string, these functions can do that

number_format 

https://www.php.net/manual/en/function.number-format.php

sprintf 

https://www.php.net/manual/en/function.sprintf.php

Keep in mind how you want to handle rounding, as number_format will round the number too. You may have specific requirements for rounding.

round 

https://www.php.net/manual/en/function.round.php

You could also try using bc math to handle your calcs

https://www.php.net/manual/en/book.bc.php

  • Related