Programming logic for income tax calculation

泄露秘密 提交于 2019-12-05 21:39:13
function get_taxed_salary($salary){
  if ($salary <= 150 ){
    return $salary;
  };
  if ($salary <= 650){
    return ( 0.9 * $salary - 15.0 );
  };

 ...
}

later in your code you use that function like:

$taxed_salary = get_taxed_salary($salary);

In most countries, that is not how tax works - you don't pay tax at a certain percent based on how much you earn. If that was the case then people who's wages were just above a tax bracket would have a net income after tax lower than someone who earnt just under a tax bracket.

How it really works: you pay tax at different percentages, for each part of your income that falls within each tax bracket. So if you earn $11,000 and there is a tax bracket from 0 to 10,000 and another from 10,000 up to 20,000 then the first 10k earned will be taxed at the first bracket's rate and the remaining 1k will be taxed at the higher rate of the second tax bracket.

Code to calculate tax in this manner:

//the tops of each tax band
$band1_top = 14000;
$band2_top = 48000;
$band3_top = 70000;
//no top of band 4

//the tax rates of each band
$band1_rate = 0.105;
$band2_rate = 0.175;
$band3_rate = 0.30;
$band4_rate = 0.33;

$starting_income = $income = 71000; //set this to your income

$band1 = $band2 = $band3 = $band4 = 0;

if($income > $band3_top) {
    $band4 = ($income - $band3_top) * $band4_rate;
    $income = $band3_top;
}

if($income > $band2_top) {
    $band3 = ($income - $band2_top) * $band3_rate;
    $income = $band2_top;
}

if($income > $band1_top) {
    $band2 = ($income - $band1_top) * $band2_rate;
    $income = $band1_top;
}

$band1 = $income * $band1_rate;

$total_tax_paid = $band1 + $band2 + $band3 + $band4;

echo "Tax paid on $starting_income is $total_tax_paid";

?>

You should learn basic PHP. The solution is trivial.

function getTax($salary) {
    $percent = 0;
    $subt = 0;

    if ($salary >= 0 && $salary <= 150) {
        $percent = 10;
        $subt = 15;
    } elseif ($salary >= 151 && $salary <= 650) {
        ...
    } ...

    // do calculations here, ex:
    $final = $salary * $percent / 100 - $subt;
    return $final;
}

Edit: Thank you Constantin for the function reminder

Check this, logic in PHP & Perl.

function calc_tax($salary) {
    $params = array(array(5001,662.50), array(3551,412.5), array(2351,235.00), array(1401,117.50), array(651,47.50), array(151,15.00), array(0,0));

    foreach ($params as $p) {
        if($salary >= $p[0]) {
            return $p[1];
        }
    }

    return 0;
}

foreach (array(100,150,3500,8900) as $sal) {
    echo $sal."==".calc_tax($sal)."\n";
}

Perl:

sub calc_tax {
    my $sal = shift;
    my @params = ( [ 5001, 662.50 ], [ 3551, 412.5 ], [ 2351, 235.00 ], [ 1401, 117.50 ], [ 651, 47.50 ], [ 151, 15.00 ], [ 0, 0 ] );

    foreach $p (@params) {
        if ( $sal >= $p->[0] ) {
            return $p->[1];
        }
    }

    return 0;
}

foreach my $sal (qw[160 3200 8900]) {
    print $sal,"==",calc_tax($sal),"\n";
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!