Kwoty wykazane w fakturze zaokrągla się zawsze do pełnych groszy. Końcówki poniżej 0,5 grosza pomija się, natomiast końcówki od 0,5 grosza i więcej zaokrągla się do 1 grosza.
// Tak ma być
$tax = 17.23;
$tax = 17.23499;
$tax = 17.734956; // Powinno być 17.73 zostawiamy 17.734 i zaokrąglamy czyli 17.73
echo number_format($tax, 2) . "</br>";
echo round(explode('.', "" . ($tax * 1000))[0] / 1000, 2) . "</br>";
// Tak jest +1 grosz za dużo (jakoś tak)
$tax = 17.23;
$tax = 17.23499;
$tax = 17.734956; // Będzie 17.74
echo round(round($tax, 3), 2) . "</br>";
// Obliczanie kwoty vat dla decimal
$brutto = 21.81;
$vat = 23;
$netto = number_format($brutto / (1 + $vat / 100), 2, ".", "");
echo "netto: $netto, vat: " . ($brutto - $netto) . ", brutto: $brutto </br>";
$netto = 17.73;
$vat = 23;
$brutto = number_format($netto * (1 + $vat / 100), 2, ".", "");
echo "netto: $netto, vat: " . ($brutto - $netto) . ", brutto: $brutto </br>";
?>
<?php
namespace App\Validate;
class Vat
{
public $tax = 17.734956;
/**
* Round vat tax (cut below 0.5 cent round to 1 cent equals and above 0.5 cent).
*
* @param float $tax
* @return float
*/
function round($tax)
{
return number_format($tax, 2);
}
/**
* Round vat tax (cut below 0.5 cent round to 1 cent equals and above 0.5 cent).
*
* @param float $tax
* @return float
*/
function roundStr($tax)
{
return round(explode('.', "" . ($tax * 1000))[0] / 1000, 2);
}
/**
* Round vat tax (invalid method +1 cent).
*
* @param float $tax
* @return float
*/
function roundSafe($tax)
{
// $tax = 11.0349;
return round(round($tax, 3), 2);
}
}