Please note that a 10% tax applies to all items except food. In addition, an additional tax of 5% applies to imported goods.
If the cost of the music CD is 12.49. The tax on this item will be 1,499. If the cost of the imported perfume bottle is 47.50, the tax on this item will be 7.125
There is a policy that states that taxes on an item should be rounded to the nearest 0.05. Therefore, 1.499 should be rounded to 1.5 and 7.125 should be rounded to 7.25.
The above rounding requirement can be achieved using logic:
(float) Math.ceil(taxAmount / 0.05f) * 0.05f;
Adding tax to the value of the goods gives:
music CD: 12.49 + 1.5 = 13.99 imported candy box: 47.50 + 7.25 = 54.65.
I ran into a problem for the following input:
If the cost of the imported candy box is 11.85, the tax will be 0.5925
Using the rounding policy, the tax after rounding will be 0.6.
When we add 11.85 + 0.6, which are floats, we get the result as 12.450001. Unlike other inputs, this particular input gives many decimal places, not 2 decimal places on other outputs.
I tried using BigDecimal instead of float to save all values with a scale set to 2 decimal places. The problem with this approach is that bigDecimal will throw an exception for some cases if no rounding policy is specified. Providing a rounding policy for BigDecimal results in the total amount of the item’s value and applicable tax being rounded up using the rounding policy provided by BigDecimal, thereby changing the required result.