Java 工具类:数字左补0

有朋友给出更6的写法:

[img]http://dl2.iteye.com/upload/attachment/0116/4063/8976f6a7-5e3e-30cf-9236-5d3fabedd519.png[/img]

DecimalFormat 是 NumberFormat 的一个具体子类,用于格式化十进制数字。

常用符号含义:

0 一个数字
# 一个数字,不包括 0
. 小数的分隔符的占位符


public static void main(String[] args) {
System.out.println(initString('0', 5));
//生成五位的字符串
System.out.println(intToString(45, 5));
//个数化的数字为45
}

public static String intToString(int n, int l) {
DecimalFormat decimalFormat = new DecimalFormat(initString(
'0', l));
return decimalFormat.format(n);
}

public static String initString(char ch, int length) {
if (length < 0)
return "";
char chars[] = new char[length];
for (int i = 0; i < length; i++)
chars[i] = ch;
return new String(chars);
}

output:
00000
00045
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值