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.
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.
The $ is literally the dollar sign while the % is the beginning of the argument %,.2f (a float with 2 decimals and a comma)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With