Best rounding method to nearest 0.05 in java

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.

+4
source share
3 answers

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

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

use long (like cents)

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

Despite the fact that most investment banks and funds using int , long or BigDecimal often use double , use double , because as soon as you understand how to use them safely, they are simpler (longer) and faster (than BigDecimal )

+7
source

Save monetary amounts as integers (for example, the number of cents).

This is a common problem - Google will give you a lot of great explanations.

Here is one of them: Why not use Double or Float to represent the currency?

+3
source

This seems to be part of a well-known interview problem with the text. The point is to understand that you need to calculate taxes and round this amount to the top 0.05 to calculate rounding. I used this groovy script

 import java.math.* def myRound(BigDecimal d){ def scale = new BigDecimal("0.05") def y = d.divide(scale) println "y :$y" // def q = y.round(new MathContext(0,RoundingMode.UP)) def q = y.setScale(0,BigDecimal.ROUND_UP) println "q :$q (intero)" def z = q.multiply(scale) println "z :$z" return z } def taxBy(BigDecimal d,BigDecimal t){ return myRound(d.multiply(t)) } def importTax(BigDecimal d){ return taxBy(d,new BigDecimal("0.15")) } def importBaseTax(BigDecimal b){ return taxBy(d,new BigDecimal("0.05")) } ip = new BigDecimal("27.99") ipt = importTax(ip) println "${ip}-->${ipt} = ${ip+ipt}" 

I hope this will be helpful!

0
source

All Articles