• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HOw to convert BigDecimal to Double?

 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The BigDecimal API lists a doubleValue() method.
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob.

Anu - as suggested please use like below
 
Stephan van Hulst
Bartender
Pie
Posts: 6128
74
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 6128
74
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hardly a “beginning” question. Moving.
 
Winston Gutkowski
Bartender
Pie
Posts: 10509
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Double.parseDouble(BigDecimal.toString());
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic