3

Suppose I have a decimal value in Java that represents money.

What's the best way to round these values?

For example, if I have a value that's calculated based on a tax rate and I end up with a result of, say, 5.3999999999999995 as the tax amount, should I round it to 2 decimal places simply by doing this:

double d = 5.3999999999999995
BigDecimal bd = new BigDecimal(d).setScale(2, RoundingMode.HALF_EVEN);
d = bd.doubleValue();

to produce the currency value:

5.40
Tom Currency
  • 31
  • 1
  • 1
  • 2
  • 8
    The exact formula used to round values is typically specified in a contract that has some legal authority. Nevertheless, you should *never* use `double`s to represent amounts of money. – Laurent Pireyn May 05 '11 at 21:04
  • I see. Why should you not use `double`s? `Float`s are better? I thought it's better to have more precision rather than less - and therefore to use `double`s as much as possible. But I'm happy to be corrected. – Tom Currency May 05 '11 at 21:12
  • 1
    float is not the way to go either – Woot4Moo May 05 '11 at 21:15
  • @Woot4Moo: Any particular reason? – Tom Currency May 05 '11 at 21:20
  • @Tom float is inherently inaccurate. this article may be of help. http://support.microsoft.com/kb/125056 the basic gist is a computer doesn't know how to represent a fraction of an integer. – Woot4Moo May 05 '11 at 21:25
  • Thanks for the link. Didn't know that. Good thing I found this out in time. – Tom Currency May 05 '11 at 21:30
  • Rounding rules are also currency-specific. – biziclop May 05 '11 at 21:31
  • @Tom: No, binary floats are absolutely not meant or fit to be used for money. They were designed mainly for numerical application (engineering and science). Read more here: http://floating-point-gui.de/ – Michael Borgwardt May 06 '11 at 07:37
  • 1
    @Woot4Moo: computers can represent fractions just fine, and floats do exactly that. Problems only appear when people expect the behaviour of decimal fractions while using a binary format. Nobody is suprised when there's problems representing 1/3 exactly. – Michael Borgwardt May 06 '11 at 07:39

4 Answers4

7

Most applications that calculate money don't use floating point (double, float); they use integer amounts representing a smaller unit. For example in $USD, money can be represented in pennies which is 1/100 of a dollar.

For better accuracy, you may want to have an integer represent 1E-03 ("milli-dollars") or 1E-06. This depends on issues such as interest calculations and your level of precision.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • 1
    Is it bad to use floating point for money? I thought that's exactly what floating point is for? Feel free to correct me - I really don't know. – Tom Currency May 05 '11 at 21:19
  • 4
    yes, it's bad to use floating point for money. No, floating point numbers were not developed to represent money. I think scientific computing had far more to do with it. – duffymo May 05 '11 at 21:21
  • Floating points give you 3.4444444444 dollars, hard to cash that – bwawok May 05 '11 at 21:45
  • 1
    +1 for not using float or double for currency. Much better to use an integer and think of dollars as cents instead. – hooknc May 05 '11 at 21:49
  • Integers are only a nasty hack for when you don't have something like BigDecimal - which Tom is ironically already using, just not completely. – Michael Borgwardt May 06 '11 at 07:44
4

I agree with @Laurent Pireyn that you should round according to the contract. For example, the IRS has you round to the nearest dollar. They say

You can round off cents to whole dollars on your return. If you do round to whole dollars, you must round all amounts. To round, drop amounts under 50 cents and increase amounts from 50 to 99 cents to the next dollar. For example, $1.39 becomes $1 and $2.50 becomes $3

Nice use of RoundingMode.HALF_EVEN. That eliminates the need for you to write and test an function.

If this actually is a tax rate for the IRS, I think that RoundingMode.HALF_UP would be correct.

rajah9
  • 11,645
  • 5
  • 44
  • 57
2

You're already using BigDecimal for the rounding, you're already using a rounding mode that is well-suited for financial applications unless there are specific requirements or regulations that proscribe a different one.

The only thing you absolutely MUST do is take the last step and use BigDecimal to represent the money values throughout your application. That's exactly what it's for, and it's much better suited to that task than double since it can represent decimal fractions exactly as expected.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
1

Money is usually rounded using the "round half to even" rule (also called "banker's rounding). It says that ties are broken by choosing the closest even digit. This can be shown to eliminate bias in rounding for both positive and negative values. (It's not the only rounding rule that has this property, but it's a nice property to have when dealing with money.)

I don't think that this rule is available in the standard API, although it isn't hard to code it up.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • 5
    The rule is very much available in the standard API - twice, actually: http://download.oracle.com/javase/6/docs/api/java/math/BigDecimal.html#ROUND_HALF_EVEN and http://download.oracle.com/javase/6/docs/api/java/math/RoundingMode.html#HALF_EVEN – Michael Borgwardt May 05 '11 at 21:12
  • Am I correct that the "round half to even" rule is what I am using in the example above and can simply stick with the above code to do "banker's rounding"? – Tom Currency May 05 '11 at 21:17
  • @Michael - Thanks. I should have dug through the docs a little deeper. – Ted Hopp May 06 '11 at 04:31