-3

I'm experiencing problem while calculating PAYE. Can someone assist me on how I should be approving this.

Here are the percentage and brackets for PAYE.

Upto 24000, 10%  
Between 24001-40667, 15% 
Between 40668-57334, 20%
Above 57334, 25%
There's a relief of 2400.

Here's my code:

double grossIncome=Convert.ToDouble(txtgross.Text):

private double getTax(double gross)
{
if(gross <=24000) return(gross*0.1);
if(gross >=24001 | gross <=40667) return(gross*0.15);
if(gross >=40668 | gross <=57334) return(gross*0.2);
return (57335+(gross*0.25);
}

To calculate PAYE:.

private void btnCalculate_Click(object sender,EventArgs e)
{
PAYE=getTax(grossIncome)-2400;
txtPaye.Text=PAYE.ToString();
}

I'm getting less PAYE value.

  • Can you give an example like: When I input value x, I get value y, but I expect value z? – SomeBody Oct 23 '20 at 13:02
  • 1
    @ChristophLütjen No, it's logical or: https://learn.microsoft.com/dotnet/csharp/language-reference/operators/boolean-logical-operators#logical-or-operator- – SomeBody Oct 23 '20 at 13:06
  • 4
    PAYE is usually a progressive tax (see https://en.wikipedia.org/wiki/Progressive_tax) - your implementation is not correct for such a taxation system, as the percentages refer to bands between the values. Otherwise someone who earns 24001 would pay much more tax than someone who earns 24000. – Martin Costello Oct 23 '20 at 13:07
  • When I input 95000 I expect 15199.86 but the output is 14250 – Charles Munene Oct 23 '20 at 13:52
  • Hard coding the bracket values _is never a good idea_. – JAlex Oct 23 '20 at 15:13
  • @JAlex how do I solve this – Charles Munene Oct 23 '20 at 16:10

1 Answers1

0

Try this:


double grossIncome=Convert.ToDouble(txtgross.Text):

private double getTax(double gross)
{
    double result = 0;

    if (gross <=24000) {
      return gross*0.1;
    } else {
      result += 2400;
    }

    if (gross >=24001) {
       var firstBase = gross - 24000;
       result += firstBase * 0.15;

       if (gross <= 40667) {
        return result;
       }
    }

    var third = gross - 40667;

    result += third * 0.2;

    if (gross <= 57334) {
       return result;
    }

    var lastbase = gross - 57334;

    result += lastbase * 0.25;

    return result;
}
zaitsman
  • 8,984
  • 6
  • 47
  • 79