This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on NumberFormat

 
Higgledy Smith
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • 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:
 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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...
 
Cristian Senchiu
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • 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 ... )
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic