“IllegalFormatConversionException: d != java.lang.String” when padding number with 0s?

前端 未结 2 1492
别跟我提以往
别跟我提以往 2021-01-11 10:07

I had a perfectly working code yesterday, in the exact form of:

int lastRecord = 1;
String key = String.format(\"%08d\", Integer.toString(lastRecord));


        
相关标签:
2条回答
  • 2021-01-11 11:00

    You don't need the Integer.toString():

     newPK = String.format("%08d", lastRecord);
    

    String.format() will do the conversion and the padding.

    0 讨论(0)
  • 2021-01-11 11:05

    Very late to the party.

    If you still are facing an issue to the String.format(); code even after implementing accepted answer.

    This did not work for me:

    String.format("%08d", stringvariable);

    Because of two reasons:

    1. It will not work as d will expect for decimal point not string.
    2. Use %8d instead of %08d as formatter did not work for me if I used this %08d

    If you are using String based Variable with String data. Use this.

    String.format("%8s", stringvariable);

    0 讨论(0)
提交回复
热议问题