• 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

Question on NumberFormat

 
Ranch Hand
Posts: 193
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did not see a question posted like this one but found many similar, so I thought I'd post and ask.

This is from problem #13, Chapter 6 - from the K&B SCJP 6.0 book

My question is: Why is the output from nf.format(d) = 987.12346 and not 987.123456?

Thank you.

Higgledy


My output is:
format 987.12346
parse 987.123456


Here is my code:
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well it is clearly written in the K&B book that a maximum limit on the number of fractional digits only applies to formatting and not parsing...
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Higgledy Smith wrote:
My question is: Why is the output from nf.format(d) = 987.12346 and not 987.123456?


You set maximum of fraction digits to 5, so format() will show only 5 digits after point by rounding.
(The digits in your example are chosen to make it confusing ... looks like 5 is disappearing ... )
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic