Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best method to round up to the nearest 0.05 in java

Consider that a tax of 10% is applicable on all items except food. Also, an additional tax of of 5 % is applicable on imported items.

If the cost of a music CD is 12.49. The tax for the item will be 1.499. If the cost of an imported bottle of perfume is 47.50, The tax on the item will be 7.125

There is a policy in place which says that taxes on an item should be rounded off to the nearest 0.05. Therefore, 1.499 should be rounded off to 1.5 and 7.125 should be rounded of to 7.25.

The above rounding requirment can be achieved using the logic :

    (float) Math.ceil(taxAmount / 0.05f) * 0.05f;

Adding the tax to the cost of the item gives :

music CD : 12.49 + 1.5 = 13.99 imported box of chocolates : 47.50 + 7.25 = 54.65.

I am facing a problem for the following input :

If the cost of an imported box of chocolates 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 both floats, we get the result as 12.450001. Unlike the other inputs, this specific input gives a lot of decimal places as opposed to the 2 decimal places in the other outputs.

I tried using BigDecimal instead of float to store all the 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 a rounding policy is not specified. Providing a rounding policy for BigDecimal causes the total of the cost of the item and the applicable tax to be rounded of using the rounding policy provided to BigDecimal, thus altering the required output.

like image 729
Chetan Kinger Avatar asked Aug 05 '12 08:08

Chetan Kinger


People also ask

Does Java round 0.5 up or down?

In mathematics, if the fractional part of the argument is greater than 0.5, it is rounded to the next highest integer. If it is less than 0.5, the argument is rounded to the next lowest integer.

What Java method rounds numbers to decimal places?

Method 1: Using format Method The decimal number can be rounded by the inbuilt format() method supported by Java.


1 Answers

You can use long instead of double to use double you can do

double d = Math.round(d * 20) / 20.0; // round up to multiple of 0.05

to use long (as cents)

long l = (l + 3) / 5 * 5;

Although its often considered best practice to use int, long or BigDecimal most investment banks and funds use double because once you understand how to use them safely, they are simpler (than long) and faster (than BigDecimal) to use.

like image 144
Peter Lawrey Avatar answered Nov 14 '22 14:11

Peter Lawrey