java 前导0,在Java中显示前导零(0)的数字?

Say that I have to add two integers one being 0001 and the other 0002. If I add it in java then I get 3 however I would like 0003. Would I have to make a loop to map out the zeros or is there an easier way.

解决方案

Don't confuse numbers with String representation of numbers. Your question revolves around the latter -- how to represent a number as a String with leading zeros, and there are several possible solutions including using a DecimalFormat object or String.format(...).

i.e.,

int myInt = 5;

String myStringRepOfInt = String.format("%05d", myInt);

System.out.println("Using String.format: " + myStringRepOfInt);

DecimalFormat decimalFormat = new DecimalFormat("00000");

System.out.println("Using DecimalFormat: " + decimalFormat.format(myInt));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值