代码之家  ›  专栏  ›  技术社区  ›  rooni

Java fOMAT字符串“%,d”和“%,d”

  •  2
  • rooni  · 技术社区  · 6 年前

    为什么是

    public static void main(String arr[]){  
      System.out.println("[" + String.format("%, d",1000000000) + "]");
    }
    

    把它写成 [ 1,000,000,000] ,号码前面有空格?

    与格式说明符“%,d”相比,“%,d”意味着什么?

    更新: 有谁能在jls上引用格式说明符的位置吗? "%, d" 被提及。

    2 回复  |  直到 6 年前
        1
  •  5
  •   user2864740 Heinzi    6 年前

    "%, d" 表示打印的是1个空格,然后是带逗号的整数( [ 1,000,000,000] )

    "%,d" 意味着您正在打印带逗号的整数( [1,000,000,000] )

    "%d" 表示打印的是不带逗号的整数( [1000000000] )

        2
  •  0
  •   MyTwoCents    6 年前

    当你沿着这条线跑的时候

    // extra space in front with number formatted
    System.out.println(String.format("%, d",1000000000));  
    // number formatted with ,
    System.out.println(String.format("%,d",1000000000));
    // just number
    System.out.println(String.format("%d",1000000000));
    
    OUTPUT:
    
     1,000,000,000
    1,000,000,000
    1000000000