For some reason in Java ME I can't use String.format()
, so what is the best way to achieve a %2d
format in Java ME with an int
? I read that the modulo opera开发者_开发百科tor can be used as a trick to achieve this, but how is that possible?
How difficult the code is depends on your assumptions on the values the integer can take.
If int x
is in the range [0, 100) (best case):
String result = (x < 10 ? "0" : "") + x;
modulo can be used as a trick to achieve this
For non-negative value, value%10
would give you the first digit, (value/10)%10
would give second, (value/100)%10
would give third digit etc
精彩评论