This week's book giveaway is in the JDBC and Relational Databases forum.
We're giving away four copies of Murach's MySQL and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • 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

Arithmetic operations with large numbers

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can i do arithmetic operations with large numbers in java?
when i use double, it returns the result with E, can i avoid this way of representation?
simple multiplication like 154.2 * 100000 giving result as 1.5419999999999998E7

Please help
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java has two classes for working with large integer and floating-point numbers. They're called java.math.BigInteger and java.math.BigDecimal, respectively.
 
Devi Hari
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ulf Dittmer:
Java has two classes for working with large integer and floating-point numbers. They're called java.math.BigInteger and java.math.BigDecimal, respectively.




Hi, thanks for the reply.
I've already tried using BigDecimal, but got problems with precision


this is the code :

double dAmnt = 154.20;
double amt = 1000000000;

BigDecimal bd1 = new BigDecimal(dAmnt);
BigDecimal bd2 = new BigDecimal(amt);
BigDecimal bd3 = bd1.multiply(bd2);
System.out.println("Bigdecimal == "+bd3);

I am getting the result as 154199999999.99998863131622783839702606201171875000000000
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is that you are constructing your BigDecimal from a "double". But the numbers you are trying to use cannot be perfectly represented by a double, so are wrong before ever becomeing a BigDecimal. To confirm this, print out the values of your BigDecimals, before the multiplication.

Instead, you should avoid floating-point (e.g. "double") altogether and construct your BigDecimal from a String or from a BigInteger. See the API for details of these constructors.

Any attempt to use floating-point arithmetic in combination with BigDecimal or BigInteger is likely to end in disappointment.
 
Devi Hari
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Peter Chase:
The problem is that you are constructing your BigDecimal from a "double". But the numbers you are trying to use cannot be perfectly represented by a double, so are wrong before ever becomeing a BigDecimal. To confirm this, print out the values of your BigDecimals, before the multiplication.

Instead, you should avoid floating-point (e.g. "double") altogether and construct your BigDecimal from a String or from a BigInteger. See the API for details of these constructors.

Any attempt to use floating-point arithmetic in combination with BigDecimal or BigInteger is likely to end in disappointment.





That solved the problem.. Thanks a lot!!!
 
Ever since I found this suit I've felt strange new needs. And a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic