go to previous page   go to home page   go to next page highlighting

Answer:

No.


Groups of Three

public class Fantasy
{
  public static void main ( String[] args )
  {
    System.out.printf("My on-line Business%n");
    System.out.printf("Year\t\tProfit%n");
    System.out.printf("----\t\t------%n");
    System.out.printf("%4d\t %,12.0f%n", 2017, -519284.34);  
    System.out.printf("%4d\t %,12.0f%n", 2018, 12764521.19);  
    System.out.printf("%4d\t %,12.0f%n", 2019, 23972987.27);  
    System.out.printf("%4d\t %,12.0f%n", 2020, 39552784.03);  
  }
}

To put commas between groups of three digits, put a comma after the percent sign. The \t inserts a tab character. The program outputs (in the US):

My on-line Business
Year            Profit
----            ------
2017         -519,284
2018       12,764,521
2019       23,972,987
2020       39,552,784 

In other countries, where the default locale has been set appropriately, characters other than comma are used to separate groups.

To format money amounts including a currency symbol, use the DecimalFormat class, discussed in the previous chapter.


QUESTION 7:

What does the following fragment write?

System.out.printf("%4d\t $%,1.0f%n", 2020, 39552784.03);  


go to previous page   go to home page   go to next page