1

我对使用 format() 和 printf 方法的字符串格式非常陌生。我已经阅读了 Oracle 网站上的教程,但发现它非常混乱。所以决定尝试一些例子。我得到了这个样本并将输出理解为124.00

public class TestStringFormatter {
  public static void main(String[] args){
    /* I do understand % - denotes start of instruction
       , is the flag
       6 - denotes width
       2 - Denotes precision
       f - Type */  
    String s = String.format("%,6.2f",124.000) ;
    System.out.printf(s);
  }
}

我无法理解的是,标志是什么以及它是如何在这种格式中使用的?有人可以解释这个例子中标志“,”的用法吗?

4

1 回答 1

1

逗号标志表示逗号将用于分隔千位,至少在美国是这样。在其他国家/地区,它将使用在这些国家/地区更有意义的分隔符。例如,123使用逗号标志格式化会 yield 123123456789使用逗号标志格式化会 yield 123,456,789

于 2013-07-22T19:58:38.677 回答