File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes HOw to convert BigDecimal to Double? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "HOw to convert BigDecimal to Double?" Watch "HOw to convert BigDecimal to Double?" New topic
Author

HOw to convert BigDecimal to Double?

Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
HI all,
I am trying to convert BigDecimal to Double.
I want to convert BigDecimal to Double, without calling new Double(...)

But, I am not sure how to do it.

please help.


With Regards,
Anu
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
The BigDecimal API lists a doubleValue() method.
Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
John Jai wrote:The BigDecimal API lists a doubleValue() method.

Thanks for the reply,
YEs, I saw that, but, I want to convert to Double. Not to primitive type.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Then use Double.valueOf(bd.doubleValue()). This will still create a new Double object though, as Double and Float objects are not cached like some instances of the other wrapper classes. This makes perfect sense; for Integer, the number of values between -128 and 127 is fixed at 256. For Double and Float there are infinite values between these bounds.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Rob Spoor wrote:This will still create a new Double object though, as Double and Float objects are not cached like some instances of the other wrapper classes.

But the API says like
be used in preference to the constructor Double(double), as this method is likely to yield significantly better space and time performance by caching frequently requested values.

Any thoughts on that?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

They either copied that comment from the other primitive classes, or have it there for future use. Anyway, right now it's a lie because this is the current implementation (taken from src.zip):
I definitely don't see any caching there.

Still, it's good to use valueOf, because future Java versions may add caching of some values (like 0.0, NaN, +infinity, -infinity).
Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
Thanks for the reply.

Is this a good practise if i code like this (calling a method with in the constructor):


please comment.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Thanks Rob.

Anu - as suggested please use like below
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

You don't even have to use the valueOf method directly. Java will automatically use it if you use autoboxing. Try:

But why do you want an object instead of the primitive?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Stephan van Hulst wrote:You don't even have to use the valueOf method directly. Java will automatically use it if you use autoboxing.

How to determine which method is internally called during autoboxing in Java. Say if I want to make sure valueOf(double) is called and not the constructor new Double(double). Could you please share a source.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

If you want to be absolutely sure the code uses valueOf(), you need to write valueOf(). Boxing makes no guarantee. In practice however, usually the valueOf() method is called. However, I can't think of a lot of situations in which you want to be absolutely sure the code uses valueOf().
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
Hardly a “beginning” question. Moving.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

John Jai wrote:How to determine which method is internally called during autoboxing in Java.
You can't, except possibly with a profiler or debugger that shows which method is being called.

Say if I want to make sure valueOf(double) is called and not the constructor new Double(double).
Now why would you want to do that John? You should know better by now.

My personal take is that I would use the autoboxing method, for the simple reason that it's not cast in stone, and is likely to be changed very quickly if a better option (or a bug) is found.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Anu satya
Ranch Hand

Joined: Mar 17, 2005
Posts: 146
HI Thanks all for the reply.
I have one final question:

if I do like this:


will the above code create two objects of type Double?
Please comment!
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
It depends on the method which returns you the double.

From the above posts, in my opinion you better use

where decimal is the big decimal reference.
Giancarlo Migliore
Greenhorn

Joined: May 10, 2013
Posts: 1
Double.parseDouble(BigDecimal.toString());
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Giancarlo Migliore wrote:Double.parseDouble(BigDecimal.toString());


No. That's wrong.

Why revive a thread after a year and a half to post an incorrect answer when the correct answer was provided long ago?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HOw to convert BigDecimal to Double?