Java Five-Oh #4: Printf
For those old skool C/C++ programmers Java 1.5 introduced a new formatting method to the PrintStream class similar to the old C printf function. The sample code below formats a float value to display only two decimal places:
out.printf("$%.2f", 12.3); // $12.30
A friend of mine saw the code above and thought it was a typo. No it is not a typo. The printf method should be familiar to a lot of programmers, it can format integers, floats, string, and data/time objects. The PrintStream printf method is shorthand for the Formatter class. All of the formatting functionality of the pritnf method is made available in the Formatter class. Use the Fromatter class to format strings that are not intended to be written out to a PrintStream but perhaps managed in a a StringBuffer or other objects.