• Post Reply Bookmark Topic Watch Topic
  • New Topic

ToString method formatting  RSS feed

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can remove '%' from the format method and append after out of that method like

 
Saloon Keeper
Posts: 4095
48
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To print an actual percent sign you need two of them ".....%%...."
 
Marshal
Posts: 58823
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 . . .

 
Saloon Keeper
Posts: 8720
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 58823
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Stephan van Hulst
Saloon Keeper
Posts: 8720
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:one or more decimal digits

 
Rancher
Posts: 3441
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This:

prints this:
79%
 
Stephan van Hulst
Saloon Keeper
Posts: 8720
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just tested it, it prints an integer. I'm quite surprised, because that's not what the documentation specifies.
 
Dave Tolls
Rancher
Posts: 3441
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Eirik Lauritsen
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Bartender
Posts: 19353
86
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Campbell Ritchie
Marshal
Posts: 58823
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Tim Holloway
Bartender
Posts: 19353
86
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Campbell Ritchie
Marshal
Posts: 58823
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Holloway
Bartender
Posts: 19353
86
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Campbell Ritchie
Marshal
Posts: 58823
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree: that is what you expect and that appears to be what you get. But it isn't what the documentation says.
 
Sheriff
Posts: 4822
135
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!