• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Double to BigDecimal Conversion problems

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have got 2 questions here. I would like to know why the following conversion is messed up.



As you may see, the BigDecimal is way off the mark. Why is it so?

The second question is, why is it so inconvenient to perform arithmetic on BigDecimals? I was being lazy and used double to calculate and printed as a BigDecimal so that I can see the full result of my calculation. Printing a double results in use of exponential notation. The arithmetic part is a bit complicated as well and I don't want to make too many function calls which I would end up doing if I used numbers of "BigDecimal" type.

Can you suggest an alternative?

Thanks,
Jason.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you check the API documentation for BigDecimal's constructors, you will see the difference between creating an instance using a double (whose precision has already been compromised by forcing it into IEEE 754 standards) and creating an instance using a String (which can be translated accurately). This is illustrated with the code below.

BigDecimal instances are objects -- not simple values. So you need to perform calculations using method calls -- not simple operators. That's just the price of precision over IEEE 754 standards.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by marc weber:
BigDecimal instances are objects -- not simple values. So you need to perform calculations using method calls -- not simple operators. That's just the price of precision over IEEE 754 standards.


Actually, it may be possible that in Java 7 BigInteger and BigDecimal will support "normal" mathematical operations. They would be special cases just like String, the only class to date to support operators.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Prime:
... Actually, it may be possible that in Java 7 BigInteger and BigDecimal will support "normal" mathematical operations...


Interesting. I hadn't heard this, but it sounds like something Sun would pursue (similar to unboxing wrapper instances for arithmetic).

But until then, we need to use methods.
 
Jason Bourne
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

I wonder if that Java 7 comment was a joke. Either way, thanks for your replies. I will live with the problem, for now.

Thanks,
Jason.
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, it is what I read in a presentation found here. The PDF itself is found here.

Please note that these are only proposals at the moment.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Prime:
Actually, it is what I read in a presentation found here. The PDF itself is found here...


Cool. Thanks for the links!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic