aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on NumberFormat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on NumberFormat" Watch "Question on NumberFormat" New topic

Question on NumberFormat

Higgledy Smith
Ranch Hand

Joined: Mar 07, 2006
Posts: 192
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.


My output is:
format 987.12346
parse 987.123456

Here is my code:
Ankit Garg

Joined: Aug 03, 2008
Posts: 9280

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...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Cristian Senchiu
Ranch Hand

Joined: Feb 08, 2009
Posts: 40
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 ... )
I agree. Here's the link:
subject: Question on NumberFormat
Similar Threads
NumberFormat Problem
nit w/ chap.6 q.13 S&B
Confused with answers from (K&B) Chapter 6: Question 13
NuberFormat format and parse doubt
Discussing errata for K&B, SCJP 6