要求:将任意位数字输出为定常格式的字符串,如001,0001等等。
package com;
import java.text.DecimalFormat;
public class FormatNumber {
/**
* 根据参数生成输出样式
*
* @param n
* @return
*/
private String initStyle(int n) {
String str = "";
for (int i = 0; i < n; i++) {
str = str + "0";
}
return str;
}
/**
* 测试
*
* @param args
*/
public static void main(String[] args) {
FormatNumber fn = new FormatNumber();
String style = fn.initStyle(3);
DecimalFormat df1 = new DecimalFormat(style);
for (int i = 0; i < 11; i++) {
System.out.println(df1.format(i));
}
}
}
输出结果:
000
001
002
003
004
005
006
007
008
009
010
参考网页:
1、
使用java.text包格式化数字和日期
2、
眀海棠文集之数据格式化1.0
3、
Merlin 的魔力: 格式化数值和货币
4、
java中的格式化输出(类似C语言的printf)
5、
Java格式化输出数字
6、
java中格式化输出数字
posted on 2007-09-06 16:10
CoderDream 阅读(130)
评论(0) 编辑 收藏 所属分类:
经验点滴