2016-07-29 106 views
-1

我有所得稅C#所得稅計算器

20% 
40% 
45% 

所以不同的速率3個文本框之間

£11,000 - £43,000 20% 
£43,001 - £150,000 40% 
150,000+   45% 

所以計算£11000£43000之間我寫了如果稅收聲明

if(Salary > decimal.Parse(noTaxThreshold) && Salary <= decimal.Parse(pc20TaxLimit)) 
      { 
       taxableIncome = Salary - decimal.Parse(noTaxThreshold); 
       annualTax = taxableIncome * decimal.Parse(lowerTaxPc); 
       tbATax.Text = annualTax.ToString(); 
      } 
      else 
      { 
       tbANetPay.Text = tbAGrossPay.Text; 
       tbATax.Text = "0.00"; 
      } 

我該如何去弄清楚其他金額,例如,如果工資是200,000英鎊pe r年,那麼這將有3個不同的所得稅率。

+1

爲什麼你必須解析你的noTaxThreshold變量?它不是小數? – LarsTech

+1

只是一個簡單的if-then-else .... – 2016-07-29 14:14:51

+0

我做了一個SqlCommand從另一個SQL表中獲取稅務信息,然後將值存儲在變量中,但無法讓它工作,除非我使用了字符串,所以我不得不解析。目前的門檻是11,000.00英鎊,所以我認爲小數比雙倍會更好。 –

回答

0

您可能會發現更容易向後工作和折扣你已經在計算稅額時總是將其更改爲小數。如何:

public decimal CalculateTax(decimal salary) { 
    const int topRateThreshold = 150000; 
    const int higherRateThreshold = 43000; 
    const int basicRateThreshold = 11000; 

    const decimal topRateFactor = 0.45M; 
    const decimal higherRateFactor = 0.40M; 
    const decimal basicRateFactor = 0.20M; 

    decimal salaryWorkingRange = salary; 

    decimal taxDue = 0; 

    if (salaryWorkingRange > topRateThreshold) { 
     taxDue += (salaryWorkingRange - topRateThreshold) * topRateFactor; 
     salaryWorkingRange = topRateThreshold; 
    } 
    if (salaryWorkingRange > higherRateThreshold) { 
     taxDue += (salaryWorkingRange - higherRateThreshold) * higherRateFactor; 
     salaryWorkingRange = higherRateThreshold; 
    } 
    if (salaryWorkingRange > basicRateThreshold) { 
     taxDue += (salaryWorkingRange - basicRateThreshold) * basicRateFactor; 
    } 

    return taxDue; 
} 
0

這聽起來像你想在一定數額的統一稅率。如果是這樣那麼這應該這樣做:

private decimal CalculateTax(int salary) 
    { 
     int noTaxThreshold = 11000; 
     int band1 = 10999; 
     int band2 = 43000; 
     int band3 = 150000; 

     if (salary < noTaxThreshold) return 0; 
     if (salary >= band3) return (salary - noTaxThreshold) * (decimal)0.45; 
     if (salary > band2) return (salary - noTaxThreshold) * (decimal)0.40; 
     if (salary > band1) return (salary - noTaxThreshold) * (decimal)0.20; 
     return 0; 
    } 

通常是通過你在帶徵稅,因此您只需支付45%的稅的部分,你earnt這是超過 150K

我用一個int爲作爲通常你得到徵稅圓形量的薪水,你可以,如果你需要

0

我實際上已經接近這一點更加動態。

public class TaxBracket 
{ 
    public int Low { get; set; } 
    public int High { get; set; } 
    public decimal Rate { get; set; } 
} 

public class TaxCalculator 
{ 
    private readonly int _taxableIncome; 
    private readonly TaxBracket[] _taxBrackets; 

    public TaxCalculator(int taxableIncome, TaxBracket[] taxBrackets) 
    { 
     _taxableIncome = taxableIncome; 
     _taxBrackets = taxBrackets; 
    } 

    public decimal Calculate() 
    { 
     var fullPayTax = 
      _taxBrackets.Where(t => t.High < _taxableIncome) 
       .Select(t => t) 
       .ToArray() 
       .Sum(taxBracket => (taxBracket.High - taxBracket.Low)*taxBracket.Rate); 

     var partialTax = 
      _taxBrackets.Where(t => t.Low <= _taxableIncome && t.High >= _taxableIncome) 
       .Select(t => (_taxableIncome - t.Low)*t.Rate) 
       .Single(); 

     return fullPayTax + partialTax; 
    } 
} 

由於我們傳入括號,它使得任何更改都更容易。

0

不要把你的代碼加載到ifs中。如果創建新的稅區會怎樣?試試這個方法:

var taxBands = new[] 
{ 
    new { Lower = 0m, Upper = 10999m, Rate = 0.0m }, 
    new { Lower = 11000m, Upper = 43000m, Rate = 0.2m }, 
    new { Lower = 43001m, Upper = 150000m, Rate = 0.4m }, 
    new { Lower = 150001m, Upper = decimal.MaxValue, Rate = 0.45m } 
}; 

var salary = 200000m; // however you get the salary figure 

var taxToBePaid = 0m; 

foreach (var band in taxBands) 
{ 
    if(salary > band.Lower) 
    { 
     var taxableAtThisRate = Math.Min(band.Upper - band.Lower, salary - band.Lower); 
     var taxThisBand = taxableAtThisRate * band.Rate; 
     taxToBePaid += taxThisBand; 
    } 
} 

Console.WriteLine(taxToBePaid); // or do whatever you want here