C#所得税计算器
本文关键字:计算器 所得税 | 更新日期: 2023-09-27 17:59:10
我有3个文本框用于不同的所得税税率
20%
40%
45%
所以在之间
£11,000 - £43,000 20%
£43,001 - £150,000 40%
150,000+ 45%
因此,为了计算11000英镑至43000英镑之间的税款,我写了一份if报表
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";
}
我该如何计算其他金额?例如,如果工资是每年20万英镑,那么这将有3种不同的所得税税率。
不要用大量的if来扰乱代码。如果建立了一个新的税阶呢?尝试这种方法:
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
听起来你想要一个固定的税率。如果是这样,那么这就应该做到:
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;
}
通常情况下,你会被分等级征税,所以你只需对超过150k 的部分缴纳45%的税
我在工资中使用了int,因为通常你会按四舍五入的金额纳税,如果你需要
public class TaxSlab
{
public int TaxId {get;set;}
public double MinRangeAmount {get;set;}
public double MaxRangeAmount {get;set;}
public int TaxRate {get;set;}
public double TaxRateAmount {get;set;}
}
class Program
{
static List<TaxSlab> taxSlab= new List<TaxSlab>();
static void LoadTaxSettings()
{
taxSlab.AddRange(new TaxSlab[]{
new TaxSlab{TaxId=1,MinRangeAmount=0,MaxRangeAmount=250000,TaxRate=0,TaxRateAmount=0},
new TaxSlab{TaxId=2,MinRangeAmount=250000,MaxRangeAmount=500000,TaxRate=10,TaxRateAmount=250000*10/100},
new TaxSlab{TaxId=3,MinRangeAmount=500000,MaxRangeAmount=1000000,TaxRate=20,TaxRateAmount=500000*20/100},
new TaxSlab{TaxId=4,MinRangeAmount=1000001,MaxRangeAmount=999999999,TaxRate=30,TaxRateAmount=2999999*30/100},
});
}
static double GetTaxAmount(double taxableAmount)
{
/*
return taxSlab.Where(x=>x.MinRangeAmount<taxableAmount).Select(x=> new {Tax=
(
(x.MinRangeAmount<taxableAmount && taxableAmount<x.MaxRangeAmount) ?
Math.Round(((taxableAmount-x.MinRangeAmount)*x.TaxRate/100),0) :
Math.Round(x.TaxRateAmount,0)
)
}).Sum(x=>x.Tax);
*/
return taxSlab.Where(x=>x.MinRangeAmount<taxableAmount).Select(x=> new {Tax=
(
(x.MinRangeAmount<taxableAmount && taxableAmount<x.MaxRangeAmount) ?
Math.Round(((taxableAmount-x.MinRangeAmount)*x.TaxRate/100),0) :
Math.Round((x.MaxRangeAmount-x.MinRangeAmount)*x.TaxRate/100,0)
)
}).Sum(x=>x.Tax);
}
static void Main(string[] args)
{
LoadTaxSettings();
Console.WriteLine("720000 : {0}" ,GetTaxAmount(720000));
Console.WriteLine("750000 : {0}" ,GetTaxAmount(750000));
Console.WriteLine("850000 : {0}" ,GetTaxAmount(850000));
Console.WriteLine("950000 : {0}" ,GetTaxAmount(950000));
}
}
您可能会发现向后计算并对您已经计算的税款进行折扣更容易
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;
}
事实上,我在处理这个问题时更为动态。
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;
}
}
由于我们在括号中通过,它使任何更改都变得更容易。