What Does $%, .2F Represent?

What Does $%, .2F Represent?

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.

0

3 Answers

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.

1

The $ is literally the dollar sign while the % is the beginning of the argument %,.2f (a float with 2 decimals and a comma)

%,.2f - format float with 2 decimal places with comma.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.