Java printf using variable field size?

后端 未结 4 1735
南方客
南方客 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:19

    maybe it's too late but putting it here for others

    int width = 5;
    int precision = 2;
    float f = 12.34567f;
    System.out.printf("%"+width+"."+precision+"f%n",f);
    

    tested in java 1.8

提交回复
热议问题