• Post Reply Bookmark Topic Watch Topic
  • New Topic

Long value in java  RSS feed

 
Aravind Prasad
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Ranchers

I am preparing the multiplication of 2 values where the answer is in trillions.

Which datatype i need to use for displaying.

250,000,000,000,000.00

when i use double, I get 2.5E and i cannot save this in the database.

Please advise.
 
Digen Mahara
Greenhorn
Posts: 23
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aravind Prasad wrote:Dear Ranchers

I am preparing the multiplication of 2 values where the answer is in trillions.

Which datatype i need to use for displaying.

250,000,000,000,000.00

when i use double, I get 2.5E and i cannot save this in the database.

Please advise.



I think this should do it for You

BigInteger class

Tutorial is as Follows

http://www.roseindia.net/java/java-biginteger/java-biginteger-long.shtml

check out for this site
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Digen Mahara wrote:I think this should do it for You

BigInteger class

BigInteger only handles integers. Try BigDecimal.
 
Palash Nandi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aravind Prasad wrote: i cannot save this in the database.


Regarding this statement. Regardless of what which object you use in Java, the Database which you are using will have its own limitations of saving integer, and how long it can be.

If it is not sufficiently long enough, you can also save it as a varchar/string in the DB since these objects will give you an accurate string representation vice versa.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aravind Prasad wrote:Dear Ranchers

I am preparing the multiplication of 2 values where the answer is in trillions.

Which datatype i need to use for displaying.

250,000,000,000,000.00

when i use double, I get 2.5E and i cannot save this in the database.

Please advise.


No, you don't get 2.5E. You're confusing the double value with how it's displayed.

The string representation that shows up when you do a simple println will be something like 2.5E14. But that's just how it got default formatted for display. The numerical value that's stored in the double isn't affected by the format. If you save that in a DB with PreparedStatement.setDouble(), then the correct value will be saved.

In Java:
  • If you need an integer value that fits in a long, use long and PreparedStatement's setLong().
  • If you need an integer value that's outside long's range, use BigDecimal and PreparedStatement's setBigDecimal().
  • If you need a value with a fractional part that fits in double's range and you don't need more precision than double offers, use double and PreparedStatement's setDouble().
  • If you need a value with a fractional part that's outside double's range or that needs more precision than double offers, use BigDecimal and PreparedStatement's setBigDecimal().


  • In the DB:
  • Define the column appropriately for the type, scale, and precision you need as per your DB's type system and particular DDL flavor.
  •  
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Palash Nandi wrote:
    Aravind Prasad wrote: i cannot save this in the database.

    If it is not sufficiently long enough, you can also save it as a varchar/string in the DB since these objects will give you an accurate string representation vice versa.


    Ick. No. Unless the numbers are ridiculously huge or the DB is an underpowered toy, it's highly unlikely that the DB's numerical types cannot handle what the OP needs.

    The main problem at the moment is that the OP is confusing a numerical value stored in a double with how it gets displayed.
     
    Palash Nandi
    Ranch Hand
    Posts: 34
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jeff Verdegan wrote:
    Ick. No.



    Jeff Verdegan wrote:
    The main problem at the moment is that the OP is confusing a numerical value stored in a double with how it gets displayed.

    Agreed!!
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!