7

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.

Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82

4 Answers4

10

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.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • How do I convert back from long to float and know the exact place where to put the decimal point? The output needs to be shown in decimal and not in long. – Chetan Kinger Aug 05 '12 at 09:05
  • I would make the assumption that there is 100 cents in the dollar. i.e. dividing by 100.0 before printing will work. Personally, I don't have a problem with using double. I wouldn't use `float` unless there really is no choice. – Peter Lawrey Aug 05 '12 at 09:11
  • To convert the decimal value into long or int, I will have to multiply it by 100? What if there are more than two digits after the decimal for the price of the item? – Chetan Kinger Aug 05 '12 at 09:14
  • What do you mean by this: `once you understand how to use them safely`? How can they be used safely? – zeller Aug 05 '12 at 09:19
  • I challenge the statement that"most investment banks and funds use double". Please provide some evidence. – user207421 Aug 05 '12 at 09:54
  • @EJP Fair point, all the investment banks I have worked at or interviewed at in London use floating point, some also use integers instead but this is rarer (I have interviewed or worked at or know people who work at almost all of them in the last ten years). Using BigDecimal is considered too slow even for situations I suspect it is not. – Peter Lawrey Aug 05 '12 at 13:27
  • Similar: http://stackoverflow.com/questions/22446677/rounded-up-to-the-nearest-0-05-dollars-failing-test – ses Mar 17 '14 at 14:08
2

Store monetary amounts as integers (e.g. a number of cents).

This is a common issue - google will give you plenty of great explanations.

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

Community
  • 1
  • 1
John3136
  • 28,809
  • 4
  • 51
  • 69
0

This seems to be part of a famous tech interview problem The point is to understand that you have to calculate the taxes and round that amount to the upper 0.05 to calculate the 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 to be useful!

Endeios
  • 911
  • 1
  • 12
  • 12
0
public static Float roundOffToNearest05(float value) {
     float counter = value /0.05f;
     long counterRounded = Math.round(counter);
     float result = counterRounded * 0.05f;
     DecimalFormat decimalFormat2 = new DecimalFormat("####0.00");
     return Float.parseFloat(decimalFormat2.format(result));
}
Dheeraj Sachan
  • 3,965
  • 2
  • 17
  • 18