Skip to content

Instantly share code, notes, and snippets.

@atomjoy
Last active October 4, 2025 08:46
Show Gist options
  • Save atomjoy/e84eabad05e6a01813ab293c8dd91780 to your computer and use it in GitHub Desktop.
Save atomjoy/e84eabad05e6a01813ab293c8dd91780 to your computer and use it in GitHub Desktop.
Zaokrąglanie podatku Vat do 1 grosza.

Zaokrąglanie Vat

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

<?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);
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment