• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

BigDecimal

 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

What exactly is the BigDecimal class used for?And what SQL datatype maps to BigDecimal? When should we go for this class?

Thanks in advance,
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As its name suggests it is for big decimals, that is values that exceed the primitive floating point ranges. Not sure about the sql question. You should use it if you need to manipulate floating point values larger then then the primitive values or the mathematical result might exceed it. I remember reading somewhere, probably Dr. Dobbs Journal, that it shouldn't be used for monetary values.

http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html

edit: I could be wrong on my recollection, but at any rate extreme care should be used when manipulating real money values. That goes without saying no matter what API you use.
[ August 16, 2006: Message edited by: Rusty Shackleford ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I remember reading somewhere, probably Dr. Dobbs Journal, that it shouldn't be used for monetary values.

I'm curious to know more about this Could you please explain why ?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49808
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the Java tutorial, in the latest edition, BigDecimal is what one is supposed to use for money. It is one of the two classes in the java.math. package, the other being BigDecimal. Both are designed for precise calculation in decimal numbers outwith the range of the primitive types.

The only way to learn about a class is to make up a little app which uses it, and play with it. Try it, and post your results on this thread.
 
Renato Losio
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Object Types Mapped to JDBC Types
http://java.sun.com/j2se/1.5.0/docs/guide/jdbc/getstart/mapping.html

java.math.BigDecimal <---> NUMERIC

Cheers,

Renato
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all!!!
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rusty Shackleford:
I remember reading somewhere, probably Dr. Dobbs Journal, that it shouldn't be used for monetary values.

Maybe you got confused - you should never use float or double for money values, you should use BigDecimal instead.

BigDecimal allows you to work with arbitrary precision decimal values. The primitive types float and double have limited precision, and you can quickly get problems with rounding errors if you don't watch out. Rounding errors are unacceptable for software that deals with money. You don't want money to disappear or magically appear, even if it is just a few cents, because of rounding errors.

So the main reason to use BigDecimal is when you need to do precise calculations in which rounding errors are not acceptable.
[ August 16, 2006: Message edited by: Jesper Young ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the main reason to use BigDecimal is when you need to do precise calculations in which rounding errors are not acceptable.


That struck me as funny for some reason. Rounding is a loss of accuracy error isn't it? So maybe BigDecimal makes rounding errors predictable?

Anyhow, BigD does money as you'd expect, like your bank or your accountant or COBOL would. If we say that behavior is "correct" then rounding with floats leads to "errors" for sure.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic