• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Double data type issue

 
Kasun Athukorala
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

This is my code....



double ctr = 0.00; //Declaration

ctr = 62176457141856560629502157223196586755079324193331.00;

out.printf("%50.2f",bla);



when i print ctr this is my output,

62176457141856560000000000000000000000000000000000.00

I'm using NetBeans 7, and windows xp sp3.
why isn't it giving the same input value??
How can i get the same output as the input??
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With a double, you can't. there are well-defined limitations on how precise they can be, and you have gone way beyond the limits.

You may want to check out item #20 on this page.

If you really need that degree of precision, you may want to check out the BigDecimal class.
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kasun Athukorala wrote:why isn't it giving the same input value??

Because double only has 64 bit to store its information. Your number is too large and it gets truncated.

How can i get the same output as the input??

By using BigDecimal. That's the only data type in Java that's accurate enough. Note that you should use the constructor that takes a String; don't create one from a double because the truncation will then happen before the BigDecimal is created.
 
Kasun Athukorala
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:With a double, you can't. there are well-defined limitations on how precise they can be, and you have gone way beyond the limits.

You may want to check out item #20 on this page.

If you really need that degree of precision, you may want to check out the BigDecimal class.



Thanks a lot for the solution mate........

Thank you both of you.........
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic