Created
May 1, 2024 15:42
-
-
Save Xor-el/41ed2c00bcaffc9b3f728d67bb32d37b to your computer and use it in GitHub Desktop.
Progressive Tax C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var taxBands = new List<TaxBand>() | |
{ | |
new() { | |
LowerLimit = 0, | |
UpperLimit = 11600, | |
RateInPercent = 10 | |
}, | |
new() { | |
LowerLimit = 11601, | |
UpperLimit = 47150, | |
RateInPercent = 12 | |
}, | |
new() { | |
LowerLimit = 47151, | |
UpperLimit = 100525, | |
RateInPercent = 22 | |
}, | |
new() { | |
LowerLimit = 100526, | |
UpperLimit = 191950, | |
RateInPercent = 24 | |
}, | |
new() { | |
LowerLimit = 191951, | |
UpperLimit = 243725, | |
RateInPercent = 32 | |
}, | |
new() { | |
LowerLimit = 243726, | |
UpperLimit = 609350, | |
RateInPercent = 35 | |
}, | |
new() { | |
LowerLimit = 609351, | |
UpperLimit = decimal.MaxValue, | |
RateInPercent = 37 | |
} | |
}; | |
decimal income = 50000; | |
var tax = CalculateProgressiveTaxWorkingImprovedD(income, taxBands); | |
Console.WriteLine($"Tax for {income} is - {tax} and Tax Percentage to Income is {GetTaxPercentRelativeToIncome(income, tax)}%"); | |
Console.ReadKey(); | |
static decimal GetTaxPercentRelativeToIncome(decimal income, decimal tax) | |
{ | |
return income > 0 ? (tax / income) * 100 : 0; | |
} | |
static decimal CalculateProgressiveTaxWorkingOriginal(decimal income, List<TaxBand> taxBands) | |
{ | |
decimal tax = 0; | |
bool isCompleted = false; | |
foreach (var band in taxBands) | |
{ | |
decimal taxableBandAmount = 0; | |
var bandTaxPercentAmount = (band.RateInPercent / 100); | |
if (band.UpperLimit >= income) | |
{ | |
taxableBandAmount = (income - band.LowerLimit); | |
isCompleted = true; | |
} | |
else | |
{ | |
taxableBandAmount = (band.UpperLimit - band.LowerLimit); | |
} | |
tax += (taxableBandAmount * bandTaxPercentAmount); | |
if (isCompleted) | |
break; | |
} | |
return tax; | |
} | |
static decimal CalculateProgressiveTaxWorkingImprovedA(decimal income, List<TaxBand> taxBands) | |
{ | |
decimal tax = 0; | |
foreach (var band in taxBands) | |
{ | |
if (income <= band.LowerLimit) | |
continue; | |
decimal taxableAmountInBand = Math.Min(income, band.UpperLimit) - band.LowerLimit; | |
decimal taxInBand = taxableAmountInBand * (band.RateInPercent / 100); | |
tax += taxInBand; | |
} | |
return tax; | |
} | |
static decimal CalculateProgressiveTaxWorkingMeImprovedB(decimal income, List<TaxBand> taxBands) | |
{ | |
decimal tax = 0; | |
foreach (var band in taxBands) | |
{ | |
if (income <= band.LowerLimit) | |
break; | |
decimal taxableAmountInBand = Math.Min(income - band.LowerLimit, band.UpperLimit - band.LowerLimit); | |
tax += taxableAmountInBand * (band.RateInPercent / 100); | |
} | |
return tax; | |
} | |
static decimal CalculateProgressiveTaxWorkingMeImprovedC(decimal income, List<TaxBand> taxBands) | |
{ | |
decimal tax = 0; | |
decimal previousUpperLimit = 0; | |
foreach (var band in taxBands) | |
{ | |
decimal lowerLimit = band.LowerLimit; | |
decimal upperLimit = band.UpperLimit; | |
decimal rateInPercent = band.RateInPercent; | |
if (income <= lowerLimit) | |
break; | |
decimal taxableAmountInBand = Math.Min(income, upperLimit) - Math.Max(previousUpperLimit, lowerLimit); | |
tax += taxableAmountInBand * (rateInPercent / 100); | |
previousUpperLimit = upperLimit; | |
} | |
return tax; | |
} | |
static decimal CalculateProgressiveTaxWorkingImprovedD(decimal income, List<TaxBand> taxBands) | |
{ | |
decimal tax = 0; | |
// Filter tax bands applicable to the given income | |
var applicableTaxBands = taxBands.Where(band => income > band.LowerLimit).ToList(); | |
foreach (var band in applicableTaxBands) | |
{ | |
//decimal taxableAmountInBand = Math.Min(income, band.UpperLimit) - band.LowerLimit; | |
decimal taxableAmountInBand = Math.Min(income - band.LowerLimit, band.UpperLimit - band.LowerLimit); | |
decimal taxInBand = taxableAmountInBand * (band.RateInPercent / 100); | |
tax += taxInBand; | |
} | |
return tax; | |
} | |
public class TaxBand | |
{ | |
public decimal LowerLimit { get; set; } | |
public decimal UpperLimit { get; set; } | |
public decimal RateInPercent { get; set; } | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment