Java – Prepend zero to byte


I'm reading file and I want to print byte value in 2 digits if it is less than 10 (example if byte=1 it should disply byte=01), I don't want to compare it like this:

        stringBuffer buf= new stringBuffer();

is there any built-in method to do this, just like the format function in vc++?

Best Solution

How about:

String twoDigits = String.format("%02d", myByte);

It's a lot closer to the C way of doing things rather than having to instantiate your own formatter.