Reputation: 714
I am currently reading the Deitel's book on Java, and came across this code in one of their programs:
public String toString()
{
return String.format( "%s: %s\n%s: $%,.2f; %s: %.2f",
"commission employee", super.toString(),
"gross sales", getGrossSales(),
"commission rate", getCommissionRate() );
}
As the title says, what does the "$%," in front of the ".2f" represent? I already know what a .2f means.
Upvotes: 4
Views: 67014
Reputation: 178293
The $
character means nothing special here. It's just a literal $
to show up in the string. The %
character takes it's usual meaning here -- to substitute with a value (here, with 2 decimal places).
Note that it's possible for the $
character to have meaning after the %
character. Please see the "Argument Index" section of the Formatter
javadocs for details.
Upvotes: 6
Reputation: 624
The $ is literally the dollar sign while the % is the beginning of the argument %,.2f (a float with 2 decimals and a comma)
Upvotes: 6