Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does $%,.2f represent?

Tags:

java

printf

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.

like image 214
Sing Sandibar Avatar asked Sep 13 '25 10:09

Sing Sandibar


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

like image 80
rgettman Avatar answered Sep 16 '25 00:09

rgettman


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

like image 31
kbickar Avatar answered Sep 16 '25 00:09

kbickar