It's not a secret anymore!*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on NumberFormat" Watch "Question on NumberFormat" New topic
Author

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.

Higgledy


My output is:
format 987.12346
parse 987.123456


Here is my code:
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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: http://aspose.com/file-tools
 
subject: Question on NumberFormat
 
Similar Threads
nit w/ chap.6 q.13 S&B
NumberFormat Problem
NuberFormat format and parse doubt
Confused with answers from (K&B) Chapter 6: Question 13
Discussing errata for K&B, SCJP 6