I had a perfectly working code yesterday, in the exact form of:
int lastRecord = 1;
String key = String.format(\"%08d\", Integer.toString(lastRecord));
You don't need the Integer.toString()
:
newPK = String.format("%08d", lastRecord);
String.format()
will do the conversion and the padding.
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:
d
will expect for decimal point not string.%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);