Forums Register Login

ToString method formatting

+Pie Number of slices to send: Send
Hello! I'm having some formatting issues with the toString method. I can't quite seem how to print a "%" after the part. How can i do that?
+Pie Number of slices to send: Send
You can remove '%' from the format method and append after out of that method like

+Pie Number of slices to send: Send
To print an actual percent sign you need two of them ".....%%...."
+Pie Number of slices to send: Send
 

Navneet Rabadiya wrote:. . .

Apart from the incorrect formatting of that line, that is a dreadful solution.

The Formatter documentation isn't clear about whether you get the decimal point printed for %.0f but it says

the integer part of m, with no leading zeroes, followed by the decimal separator followed by one or more decimal digits . . .

 
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

Stephan van Hulst wrote:one or more decimal digits

 
+Pie Number of slices to send: Send
This:

prints this:
79%
+Pie Number of slices to send: Send
I just tested it, it prints an integer. I'm quite surprised, because that's not what the documentation specifies.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.


+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:

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.
+Pie Number of slices to send: Send
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:-

$ java RadixPointDemo 3 12.345
12.345%
$ java RadixPointDemo 1 12.345
12.3%
$ java RadixPointDemo 0 12.345
12%

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.
+Pie Number of slices to send: Send
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).
+Pie Number of slices to send: Send
Agree: that is what you expect and that appears to be what you get. But it isn't what the documentation says.
+Pie Number of slices to send: Send
This line: can be simplified to this:
When you are tempted to write instead write So this code:
can be written:
or even more clearly:
Eliminate 95% of the weeds in your lawn by mowing 3 inches or higher. Then plant tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1056 times.
Similar Threads
How do I create a battlecode from the code I already created?
Question About Inheritance
JAVA- Object Refernce
random number code problem
Subtracting strings
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:52:14.