Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Double data type issue

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
lowercase baba
Posts: 12988
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22208
117
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.........
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic