开发者

how to display int as two digit in java me

开发者 https://www.devze.com 2023-04-05 12:28 出处:网络
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 t

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

精彩评论

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