| Author |
BigDecimal
|
A Kumar
Ranch Hand
Joined: Jul 04, 2004
Posts: 973
|
|
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
Joined: Jan 03, 2006
Posts: 490
|
|
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 ]
|
"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14685
|
|
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 ?
|
[My Blog]
All roads lead to JavaRanch
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32830
|
|
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
Joined: Nov 23, 2005
Posts: 99
|
|
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
|
Renato Losio - www.arsenio.it - renatoweb@arsenio.it
|
 |
A Kumar
Ranch Hand
Joined: Jul 04, 2004
Posts: 973
|
|
Thank you all!!!
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12950
|
|
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 ]
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
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.
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
 |
|
|
subject: BigDecimal
|
|
|