participate


New To Java Technology Archive - Difference between BigDecimal and Double?
<<   Back to Forum  |   Give us Feedback
This topic has 10 replies on 1 page.
aaabcdefg
Posts:365
Registered: 5/21/02
Difference between BigDecimal and Double?   
Feb 19, 2004 4:50 AM

 
Hey all,

I'm sure many of you think this is a dumb question, but after searching the forums and reading the javadocs I am still unsure of the difference between the two. I am retrieving info from a database, and I am using the ResultSetMetaData to get the type of a field. The type is returning a BigDecimal. All the methods I need to use accept a double as the parameter.

So my question is:
1) In laymen terms, what exactly is the difference between Double and BigDecimal?
2) Is there any harm in using the doubleValue of this BigDecimal in all of my methods?

Thanks for any & all help!
 
bbritta
Posts:8,810
Registered: 10/4/00
Re: Difference between BigDecimal and Double?   
Feb 19, 2004 5:06 AM (reply 1 of 10)  (In reply to original post )

 
Double holds about 17 digits (I think) of precision.

BigDecimal can hold many, many, many digits of precision at the expense of memory and speed..

BigDecimal can make your program run slower and use more memory, but if you need the precision, WTF.
 
aaabcdefg
Posts:365
Registered: 5/21/02
Re: Difference between BigDecimal and Double?   
Feb 19, 2004 5:16 AM (reply 2 of 10)  (In reply to #1 )

 
Thanks, since I don't need that much precision I believe Double will be just fine for me :)

I have a follow up question I hope you can answer:
1) I am confused about which primitives the java.sql.Types match up too. Some are obvious, like Types.INTEGER. But others are not so obvious, I listed these below. I put down what I think some of them mean, but I could be way off. Can you help me with this?

1) BIGINT = BigInteger?
2) DATALINK?
3) DECIMAL = long?
4) DISTINCT?
5) LONGVARCHAR = String?
6) NUMERIC = double?
7) REF?
8) SMALLINT = short?
9) STRUCT?
10) TINYINT?
11) VARBINARY?

I know this is a lot to ask, but could you provide any assistance? Thanks!
 
duffymo
Posts:27,863
Registered: 2/20/98
Re: Difference between BigDecimal and Double?   
Feb 19, 2004 5:17 AM (reply 3 of 10)  (In reply to #2 )

 
Depends on the database, I believe. - MOD
 
aaabcdefg
Posts:365
Registered: 5/21/02
Re: Difference between BigDecimal and Double?   
Feb 19, 2004 5:20 AM (reply 4 of 10)  (In reply to #3 )

 
Depends on the database, I believe. - MOD

- I guess that would make sense, thanks!
 
bbritta
Posts:8,810
Registered: 10/4/00
Re: Difference between BigDecimal and Double?   
Feb 19, 2004 5:25 AM (reply 5 of 10)  (In reply to #2 )

 
All drivers that I have use support getObject(). I think there is some kind of translation table built into the driver itself which gives you the approprate translation. I've seen somewhere where some driver ddidn't support some of the more esoteric field types.
 
DougDean
Posts:376
Registered: 2/11/00
Re: Difference between BigDecimal and Double?   
Feb 19, 2004 5:36 AM (reply 6 of 10)  (In reply to #2 )

 
This [url=http://java.sun.com/docs/books/tutorial/jdbc/basics/retrieving.html]link[/url] may help...
 
aaabcdefg
Posts:365
Registered: 5/21/02
Re: Difference between BigDecimal and Double?   
Feb 19, 2004 5:39 AM (reply 7 of 10)  (In reply to #6 )

 
Wow, thats a great link! The table at the bottom of that page is just what I needed. Thanks everyone for the help!
 
UlrikaJ
Posts:5,965
Registered: 5/17/03
Re: Difference between BigDecimal and Double?   
Feb 19, 2004 6:55 AM (reply 8 of 10)  (In reply to #2 )

 
Thanks, since I don't need that much precision I
believe Double will be just fine for me :)

Make sure you know the difference between double (a primitive type) and Double (a class type).
 
aaabcdefg
Posts:365
Registered: 5/21/02
Re: Difference between BigDecimal and Double?   
Feb 19, 2004 10:51 AM (reply 9 of 10)  (In reply to #8 )

 
Make sure you know the difference between double (a
primitive type) and Double (a class type).

Yeah, I do. BigInteger has a doubleValue() method to get the double primitive which my methods use. I just figured I would ask the difference between BigDecimal and Double (instead of double) to avoid all of those "Stupid newbie, one's a class and the other is a primitive" smart @$$ answers. Thanks!
 
dubwai
Posts:13,976
Registered: 11/29/00
Re: Difference between BigDecimal and Double?   
Feb 19, 2004 1:07 PM (reply 10 of 10)  (In reply to #1 )

 
Double holds about 17 digits (I think) of precision.

BigDecimal can hold many, many, many digits of
precision at the expense of memory and speed..

BigDecimal can make your program run slower and use
more memory, but if you need the precision, WTF.

Another big difference between double and BigDecimal is that doubles stores it's fraction part in binary and BigDecimal stores it's fraction part in decimal. This matters becasue there is no way to exactly represent 0.1 in binary, for example. A truncated repeating bincimal (my word) that approximates the value of 0.1 is used instead.

Note that in order to make use of BigDecimal's ability to represent these numbers properly, it is neccesary to initialize the BigDecimal with a string or a type other than double or float. If you use a double to initialize the BigDecimal, the approximation will be passed on to the BigDecimal instance.
 
This topic has 10 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 25
  • Guests : 136

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums