3v4l.org

run code in 300+ PHP versions simultaneously
<?php function money_round ($amount) { # convert to string $string = (string)$amount; ## find out if negative ## if (substr($string, 0, 1) == '-') { $negative = true; $string = substr($string, 1, strlen($string) - 1); } else { $negative = false; } # if there's no decimal point add one on the end if (substr_count($string, '.') == 0) { $string .= '.'; } # add a few trailing '0's in case there aren't enough there already $string .= '000'; # find the decimal point $dotpos = strpos($string, '.'); # separate pounds $pounds = substr($string, 0, $dotpos); # separate pence $pence = substr($string, $dotpos + 1, 2); # find out how many tenths of a penny $tenths = substr($string, $dotpos + 3, 1); if ($tenths >= 5) { $pence++; if ($pence == 100) { $pence = '0'; $pounds++; } if ($pence < 10) { $pence = '0' . $pence; } } if ($negative) { return '-' . $pounds . '.' . $pence; } else { return $pounds . '.' . $pence; } } function CalcLineTaxExc ($quantity, $unit_price, $tax_percentage) { ######################################################################################################### ## !! IMPORTANT !! READ THIS BEFORE YOU USE THIS FUNCTION ## ######################################################################################################### ## ## ## $unit_price MUST *EXCLUDE* ANY TAX AND MUST *NOT* BE ROUNDED BEFORE BEING PASSED TO THIS FUNCTION ## ## ## ## THIS FUNCTION RETURNS THE EXACT AMOUNT OF TAX TO BE ADDED (FOR THE LINE) ROUNDED TO TWO D.P. ## ## ## ## TAX IS CALCULATED BY TAKING THE (PRICE INCL. TAX * QTY) AND DEDUCTING THE (PRICE EXCL. TAX * QTY), ## ## THIS METHOD ENSURES THAT CUSTOMERS PAY THE INTENDED AMOUNT INCL. TAX PER LINE AND IS SAFE WHETHER ## ## THE CUSTOMER HAS SEEN THE PRICE INCL. OR EXCL. TAX. ## ## ## ## TAX ON SOME LINES MAY END UP A PENNY OUT WHEN COMPARED TO ($unit_price * $qty * $tax_percentage) ## ## BUT THIS IS THE CORRECT WAY TO DO IT AND ON AVERAGE THE PENNYS WILL BALANCE EACH OTHER OUT ANYWAY ## ## ## ######################################################################################################### $temp_inc_tax = $unit_price * (1 + $tax_percentage); $line_tax_amount = money_round($quantity * $temp_inc_tax) - money_round($quantity * $unit_price); $line_tax_amount = money_round($line_tax_amount); # just to be safe in case of rounding error on last calc # return $line_tax_amount; } function CalcLineTaxInc ($quantity, $unit_price, $tax_percentage) { ######################################################################################################### ## !! IMPORTANT !! READ THIS BEFORE YOU USE THIS FUNCTION ## ######################################################################################################### ## ## ## $unit_price MUST *INCLUDE* ANY TAX AND MUST *NOT* BE ROUNDED BEFORE BEING PASSED TO THIS FUNCTION ## ## ## ## THIS FUNCTION RETURNS THE EXACT AMOUNT OF TAX TO BE ADDED (FOR THE LINE) ROUNDED TO TWO D.P. ## ## ## ## TAX IS CALCULATED BY TAKING THE (PRICE INCL. TAX * QTY) AND DEDUCTING THE (PRICE EXCL. TAX * QTY), ## ## THIS METHOD ENSURES THAT CUSTOMERS PAY THE INTENDED AMOUNT INCL. TAX PER LINE AND IS SAFE WHETHER ## ## THE CUSTOMER HAS SEEN THE PRICE INCL. OR EXCL. TAX. ## ## ## ## TAX ON SOME LINES MAY END UP A PENNY OUT WHEN COMPARED TO ($unit_price * $qty * $tax_percentage) ## ## BUT THIS IS THE CORRECT WAY TO DO IT AND ON AVERAGE THE PENNYS WILL BALANCE EACH OTHER OUT ANYWAY ## ## ## ######################################################################################################### $temp_inc_tax = $unit_price; $temp_exc_tax = $temp_inc_tax / (1 + $tax_percentage); $line_tax_amount = money_round($quantity * $temp_inc_tax) - money_round($quantity * $temp_exc_tax); $line_tax_amount = money_round($line_tax_amount); # just to be safe in case of rounding error on last calc # return $line_tax_amount; } $tests = array(1,10,100,1000,5,15,105,1005,1.5,11.5,101.5,374.75,874.76,39665894.97); $results = array(); foreach($tests as $test) { $result = array(); $result['input_unit_price'] = $test; $result['tax_amount'] = CalcLineTaxExc(1, $test, 0.2); $result['input_price_incl_tax'] = $test + $result['tax_amount']; $result['price_exl_tax'] = $result['input_price_incl_tax'] - CalcLineTaxInc(1,$test,0.2); $results[] = $result; } var_dump($results);
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
array(14) { [0]=> array(4) { ["input_unit_price"]=> int(1) ["tax_amount"]=> string(4) "0.20" ["input_price_incl_tax"]=> float(1.2) ["price_exl_tax"]=> float(1.03) } [1]=> array(4) { ["input_unit_price"]=> int(10) ["tax_amount"]=> string(4) "2.00" ["input_price_incl_tax"]=> float(12) ["price_exl_tax"]=> float(10.33) } [2]=> array(4) { ["input_unit_price"]=> int(100) ["tax_amount"]=> string(5) "20.00" ["input_price_incl_tax"]=> float(120) ["price_exl_tax"]=> float(103.33) } [3]=> array(4) { ["input_unit_price"]=> int(1000) ["tax_amount"]=> string(6) "200.00" ["input_price_incl_tax"]=> float(1200) ["price_exl_tax"]=> float(1033.33) } [4]=> array(4) { ["input_unit_price"]=> int(5) ["tax_amount"]=> string(4) "1.00" ["input_price_incl_tax"]=> float(6) ["price_exl_tax"]=> float(5.17) } [5]=> array(4) { ["input_unit_price"]=> int(15) ["tax_amount"]=> string(4) "3.00" ["input_price_incl_tax"]=> float(18) ["price_exl_tax"]=> float(15.5) } [6]=> array(4) { ["input_unit_price"]=> int(105) ["tax_amount"]=> string(5) "21.00" ["input_price_incl_tax"]=> float(126) ["price_exl_tax"]=> float(108.5) } [7]=> array(4) { ["input_unit_price"]=> int(1005) ["tax_amount"]=> string(6) "201.00" ["input_price_incl_tax"]=> float(1206) ["price_exl_tax"]=> float(1038.5) } [8]=> array(4) { ["input_unit_price"]=> float(1.5) ["tax_amount"]=> string(4) "0.30" ["input_price_incl_tax"]=> float(1.8) ["price_exl_tax"]=> float(1.55) } [9]=> array(4) { ["input_unit_price"]=> float(11.5) ["tax_amount"]=> string(4) "2.30" ["input_price_incl_tax"]=> float(13.8) ["price_exl_tax"]=> float(11.88) } [10]=> array(4) { ["input_unit_price"]=> float(101.5) ["tax_amount"]=> string(5) "20.30" ["input_price_incl_tax"]=> float(121.8) ["price_exl_tax"]=> float(104.88) } [11]=> array(4) { ["input_unit_price"]=> float(374.75) ["tax_amount"]=> string(5) "74.95" ["input_price_incl_tax"]=> float(449.7) ["price_exl_tax"]=> float(387.24) } [12]=> array(4) { ["input_unit_price"]=> float(874.76) ["tax_amount"]=> string(6) "174.95" ["input_price_incl_tax"]=> float(1049.71) ["price_exl_tax"]=> float(903.9200000000001) } [13]=> array(4) { ["input_unit_price"]=> float(39665894.97) ["tax_amount"]=> string(10) "7933178.99" ["input_price_incl_tax"]=> float(47599073.96) ["price_exl_tax"]=> float(40988091.47) } }
Output for 4.3.0 - 4.3.11, 4.4.0 - 4.4.9, 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.8 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33
array(14) { [0]=> array(4) { ["input_unit_price"]=> int(1) ["tax_amount"]=> string(4) "0.20" ["input_price_incl_tax"]=> float(1.2) ["price_exl_tax"]=> float(1.03) } [1]=> array(4) { ["input_unit_price"]=> int(10) ["tax_amount"]=> string(4) "2.00" ["input_price_incl_tax"]=> float(12) ["price_exl_tax"]=> float(10.33) } [2]=> array(4) { ["input_unit_price"]=> int(100) ["tax_amount"]=> string(5) "20.00" ["input_price_incl_tax"]=> float(120) ["price_exl_tax"]=> float(103.33) } [3]=> array(4) { ["input_unit_price"]=> int(1000) ["tax_amount"]=> string(6) "200.00" ["input_price_incl_tax"]=> float(1200) ["price_exl_tax"]=> float(1033.33) } [4]=> array(4) { ["input_unit_price"]=> int(5) ["tax_amount"]=> string(4) "1.00" ["input_price_incl_tax"]=> float(6) ["price_exl_tax"]=> float(5.17) } [5]=> array(4) { ["input_unit_price"]=> int(15) ["tax_amount"]=> string(4) "3.00" ["input_price_incl_tax"]=> float(18) ["price_exl_tax"]=> float(15.5) } [6]=> array(4) { ["input_unit_price"]=> int(105) ["tax_amount"]=> string(5) "21.00" ["input_price_incl_tax"]=> float(126) ["price_exl_tax"]=> float(108.5) } [7]=> array(4) { ["input_unit_price"]=> int(1005) ["tax_amount"]=> string(6) "201.00" ["input_price_incl_tax"]=> float(1206) ["price_exl_tax"]=> float(1038.5) } [8]=> array(4) { ["input_unit_price"]=> float(1.5) ["tax_amount"]=> string(4) "0.30" ["input_price_incl_tax"]=> float(1.8) ["price_exl_tax"]=> float(1.55) } [9]=> array(4) { ["input_unit_price"]=> float(11.5) ["tax_amount"]=> string(4) "2.30" ["input_price_incl_tax"]=> float(13.8) ["price_exl_tax"]=> float(11.88) } [10]=> array(4) { ["input_unit_price"]=> float(101.5) ["tax_amount"]=> string(5) "20.30" ["input_price_incl_tax"]=> float(121.8) ["price_exl_tax"]=> float(104.88) } [11]=> array(4) { ["input_unit_price"]=> float(374.75) ["tax_amount"]=> string(5) "74.95" ["input_price_incl_tax"]=> float(449.7) ["price_exl_tax"]=> float(387.24) } [12]=> array(4) { ["input_unit_price"]=> float(874.76) ["tax_amount"]=> string(6) "174.95" ["input_price_incl_tax"]=> float(1049.71) ["price_exl_tax"]=> float(903.92) } [13]=> array(4) { ["input_unit_price"]=> float(39665894.97) ["tax_amount"]=> string(10) "7933178.99" ["input_price_incl_tax"]=> float(47599073.96) ["price_exl_tax"]=> float(40988091.47) } }

preferences:
260.03 ms | 413 KiB | 314 Q