• 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

data type for large decimals

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I store values like 1.6666666666666666666 ?
double wont work
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BigDecimal?
 
varun bihani
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to use it ?
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you already read the API javadoc?.
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Scott Johnson
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic