Java printf using variable field size?

后端 未结 4 1743
南方客
南方客 2020-11-29 06:49

I\'m just trying to convert some C code over to Java and I\'m having a little trouble with String.printf.

In C, to get a specific width based on a varia

4条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 07:11

    Declare an extra variable before using printf:

    String format = "%" + fieldSize + "d";
    System.out.printf(format, yourVariables);
    

    (this is the first solution I found from a web search)

提交回复
热议问题