• 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

NumberFormat and truncating fraction digits

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I am trying to find out why the following piece of code:


displays this output:



If the default number of fraction digits is 3, why is .5668 truncated to .57?
And why does the code display .5703 when the number of fraction digits is extended to 4?

Thanks,
Justyna W.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi it works very strange with floats

check
float fl = 123456.566f;

try to use double

bye
 
Dan Polak
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
123456.5668f is to big for float
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
float gives 6-9 digits precision, although in your example I could only use 8 digits before truncation. Just add System.out.println(">"+fl+"<"); just under your float declaration to see.

Dealing with floats is tricky in general, as I have recently found out when attempting to do accounting arithmetic (think BigDecimal).

Use a double to get the expected result.



result:

3
123,456.567
123,456.5668

Prosze bardzo!
 
reply
    Bookmark Topic Watch Topic
  • New Topic