The documentation is actually quite clear. A decimal point, and one or more decimal digits. If you want to print an integer, you need to use the integer conversion.
I would add a private int getCriticalChanceAsPercentage() that performs the multiplication and a cast. That will also make the formatting statement more easy to read.
So it would print 79.% for "...%.0f%%..." 79
I thought that is what the documentation said, but would you get a digit after the radix point with a 0 precision number?
Well, that format as given is telling the formatter to have no digits after the decimal (the '.0' part), and the docs say, in one of the bullet points:
The decimal separator will only appear if a digit follows it
ETA: I will say, I've never liked the Formatter docs. They have always confused me.
Seeing as quite a few people started to discuss the output of that spesific line I thought I could post the complete program to show what it actually prints in this instance.
Dave Tolls wrote:Well, that format as given is telling the formatter to have no digits after the decimal (the '.0' part), and the docs say, in one of the bullet points:
The decimal separator will only appear if a digit follows it
ETA: I will say, I've never liked the Formatter docs. They have always confused me.
When in doubt, just remember that the basic formatter stole its concepts from C and look up the C language documentation for "printf".
That said, I rather like the Java formatting convention where instead of absolute positioning "%d %d %s", you can specify customized positioning - "{1} {3} {2}". It's more flexible when obtaining your formatting string from an internationalized resource.
The decimal separator will only appear if a digit follows it
The only place I could find that in the documentation was after “If no flags are given...” I can see why you don't like that documentation. Maybe it was written by the same person as Scanner's.
The decimal separator will only appear if a digit follows it
The only place I could find that in the documentation was after “If no flags are given...” I can see why you don't like that documentation. Maybe it was written by the same person as Scanner's.
Well, in English - which was the language of Kernigan & Ritchie - a number that terminates with a decimal point and no fraction not only looks odd, the odds that people won't read it correctly exceed 0.. (sic)
The decimal point is a separator, not a terminator. Something that working with different languages has made me sensitive to.
Agree that the radix point isn't a terminator. Agree that writing 79. looks very strange and 79.0 doesn't. I would expect %.0f to miss out the radix point:-
System.out.format uses exactly the same tags as Formatter. But I am blowed if I can find anything in the documentation that actually says the . is omitted from %f???.0.
Actually, having had to write a printf formatter once or twice, I can tell you that one common way to parse a format marker is to set up a basic structure that contains a sign indicator, a pre-decimal and post-decimal value and a format type indicator and do a general-purpose parse rather than writing one-off point-by-point parsing code for each possible format. That's what makes strings like "%-12.2s" acceptable.
So for elements that are missing, default values are assumed, just like in an HTTP URL, the default port ":80" is assumed. Meaning that when the actual formatting is done, the strings "%12d" and "%12.0d" have both parsed down to the same post-decimal value (0).