Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes data type for large decimals Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "data type for large decimals" Watch "data type for large decimals" New topic
Author

data type for large decimals

varun bihani
Ranch Hand

Joined: Aug 11, 2003
Posts: 96
How do I store values like 1.6666666666666666666 ?
double wont work


<a href="http://www.galaxyweblinks.com" target="_blank" rel="nofollow">www.galaxyweblinks.com</a><br /><a href="http://www.freechatcode.com" target="_blank" rel="nofollow">www.freechatcode.com</a><br /><a href="http://www.onlinefamilytreemaker.com" target="_blank" rel="nofollow">www.onlinefamilytreemaker.com</a>
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
BigDecimal?
varun bihani
Ranch Hand

Joined: Aug 11, 2003
Posts: 96
how to use it ?
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Have you already read the API javadoc?.
Sanjaya Sugiarto
Ranch Hand

Joined: Mar 25, 2004
Posts: 229
First of all value 1.6666666666666666666 fits in double.

Second, the JavaDoc of BigDecimal: http://java.sun.com/j2se/1.4.2/docs/api/java/math/BigDecimal.html

Third, how to use BigDecimal:


<a href="http://www.wi.hs-furtwangen.de" target="_blank" rel="nofollow">Business Information Technology - Hochschule Furtwangen University, Germany</a>
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
[Sanjaya]First of all value 1.6666666666666666666 fits in double.

Not really. A double cannot store 20 significant digits. If you store it in a double, it'll be rounded to fewer significant digits.

[B][Sanjaya]Third, how to use BigDecimal:


[/B]

If you do this, the parameter value will be treated as a double and will be rounded for the reason stated above. To create a BigDecimal with more signficant digits than can fit in a double, you'll need to use one of the other constructors.

Also, bd.doubleValue() returns a double. Assuming you get the BigDecimal created with the full 20 digits and then call doubleValue(), you'll encounter same problem -- the value will be rounded to fit into a double. To print the BigDecimal, use the toString() method instead:


[ October 26, 2006: Message edited by: Scott Johnson ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: data type for large decimals