This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes printf string formatting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "printf string formatting" Watch "printf string formatting" New topic
Author

printf string formatting

O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

In the following program



The output is



Can someone please explain how a double value (i.e. 123.456) is converted to a boolean (ie. true)



Thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 20057
    
  44


It's all explained, in the JavaDoc of the java.util.Formatter class, which is the class that is used to format the text.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
O. Ziggy
Ranch Hand

Joined: Oct 02, 2005
Posts: 430

Yes i saw the description in the API.

If the argument arg is null, then the result is "false". If arg is a boolean or Boolean, then the result is the string returned by String.valueOf(arg). Otherwise, the result is "true".


It just seemed weird that any string can result in a value of true. For example, the following expression also prints true.

Paul Clapham
Sheriff

Joined: Oct 14, 2005
Posts: 19728
    
  10

You're better off believing what the documentation says, rather than what your intuition says. For one thing that's how the JVM is actually going to work.
 
Have you checked out Aspose?
 
subject: printf string formatting
 
It's not a secret anymore!