Sing Sandibar
Sing Sandibar

Reputation: 714

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.

Upvotes: 4

Views: 67014

Answers (3)

rgettman
rgettman

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

AllTooSir
AllTooSir

Reputation: 49402

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

Upvotes: 4

kbickar
kbickar

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

Related Questions