Click here to Skip to main content
15,077,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to calculate american Tax. The store in Florida is causing me some headaches.

There is a state tax of 6% and a city tax of 1% but only on the first $5000

It's easy to work out the tax from the price:

C#
var total = taxRates.sum(t=>t.rate * Math.Min(t.MaxAmount??Price, Price))


This works with all tax rates and types. UK tax for eg. will only have 1 tax rate in the TaxRates list and the MaxAmount will be null.

He's the issue. I have the amount including tax and I have to come up with a one-size-fits-all method of calculating the Price ex. Tax.

Any ideas?

What I have tried:

I currently work out the threshold of the rate and iterate over the rates:

I.E. consider the following example for Florida:

Max Amount       Rate
5000             1%
null             6%
null             1%   (a fake locality tax for this demo)


the first $5000 will be taxed as 8% total. I work out the amount each 'tier' is taxed and and create the tax bands as so:

Max Amount       Rate
5000             8%
null             7%   (rates with the same maxamount are aggregates)


I can now tell the band threshold:

Rate     Threshold          Before Tax
8%       5400               5000
7%       decimal.MaxValue   null


so I can now iterate through this list to find the actual tax amount.

Say we have $2700. This is not higher than the first tax band so it it all taxed at 8%. 2700 / 1.08 = $2500

10000: This is over 5400 so 5400 is taxed at 8%:
4600: this is within the next band so all is taxed at 7%
= 5000 + (4600 / 1.07)

It works but it very clunky.
Posted
Updated 2-Nov-18 1:22am
Comments
Richard MacCutchan 2-Nov-18 5:38am
   
How does 2700 plus 8% tax equal 2500? Similarly your last calculation does not look correct.
Andy Lanng 2-Nov-18 6:44am
   
first:
2500 + 8% = 2700
so
2700 / 1.08 = 2500, which is what I wrote
second :
so 5400 of the total was the 5000 charged at 8%. That means that 5400 of the 10000 has been accounted for.
this leaves 4600 to be accounted for in the next tax bracket
Richard MacCutchan 2-Nov-18 9:55am
   
I thought you were starting with the net amounts to calculate the tax.
Andy Lanng 2-Nov-18 10:56am
   
starting with the price + tax. I need to get the price. If I know the tax (i.e. 8%) then to get the price only I need to divide it by 1.08.
The issue is that when there is a limit to how much the 1% tax applies to, the total tax % will be somewhere between 7 & 8 %
Richard MacCutchan 2-Nov-18 11:54am
   
In my experience of shopping in the US they always show the net price, and add the tax at the till. Is Florida different?
Andy Lanng 5-Nov-18 3:47am
   
Florida is no different. My client is.
Richard MacCutchan 5-Nov-18 4:02am
   
I always used to think that without clients our job would be so much easier.
Andy Lanng 6-Nov-18 8:48am
   
^_^
MadMyche 2-Nov-18 8:48am
   
Is this for a physical store or will there be shipping involved?
Andy Lanng 2-Nov-18 10:52am
   
Thankfully no shipping ^_^

1 solution

C#
double amt = 5400d;
double stateTax = 6d;
double cityTax = 1d;
double cityTaxableAmnt = 5000d;
double stateTaxableAmt = double.MaxValue;

double stateTaxAmt = Math.Min(amt, stateTaxableAmt) * (stateTax * 0.01);
double cityTaxAmt  = Math.Min(amt, cityTaxableAmt) * (cityTax * 0.01);
total = amt + stateTaxAmt + cityTaxAmt;


I'll leave it as an exercise for the programmer to make this code work regardless of the state or country. Also, I would use decimal types in production code because floating point math is just an approximation, where decimal math is much more precise..
   
Comments
Andy Lanng 5-Nov-18 3:48am
   
This is included in my current solution (although I do use decimal).
I need a single solution that can take care of tax globally. One solution for all.
#realJSOP 5-Nov-18 5:25am
   
Well, not every government entity implements the same tax determination. You're going to be writing code forever. Unfortunately, you have to know all of the tax codes before you can design a class hierarchy or database schema to handle it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900