开发者

how to display int as two digit in java me

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜