Suitable data structure for calculating progressive tax

Company A has its own system that it uses to levy sales tax. Taxes are calculated in a progressive manner. E.g. if the seller sells goods worth $ 25, then for the first $ 10 tax = 8% and for the remaining $ 15 tax = 7%. Thus, the total tax = 8% of 25 + 7% of 15.

The table they use to calculate the tax is as follows

$0 - $10 8%
$11 - $50 7%
$51 - $500 6%
$501 - $10000 5%
$10001 -$1000000 4% and so on.

What data structure would you use to store this table and how would you use this data structure to encode a function float computeTaxableAmount(float amount) {}

+3
source share
1 answer

I would use an array of structures. Consider:

fields: from  to    percentage cumulative

values: 0     10    0.08       0
        10    50    0.07       0.80 (= (to-from)*percentage from row above)
        50    500   0.06       0.80 + (50-10)*0.07 = 4.00
        500   10000 0.05       4.00 + (500-50)*0.06 = 31.00
        ...

: , . , X , , X (.. from <= X < to), :

(X - from) * percentage + cumulative

.

, X, - - / , "" . ( , , , , , , , , ..)

+3

Source: https://habr.com/ru/post/1786845/


All Articles