1、例子
public static void main(String[] args) {
SimgleFormat("###,###.###",123456.789 );
SimgleFormat("00000000.####kg", 123456.789);
SimgleFormat("000000.000", 123.78);
UseApplyPatternFormat("#.###%", 0.789);
UseApplyPatternFormat("###.##", 123456.789);
UseApplyPatternFormat("0.00\u2030", 0.789);
UseApplyPatternFormat("0.00\u00A4",0.234);
}
/* 静态方法属于类所有,区别于个别对象,可以在本类或者其他
* 类使用类名和“.”运算符调用静态成员。
**/
// 使用实例化对象时这只格式化模式
static public void SimgleFormat(String pattern,double value){
// 实例化DecimalFormat对象
DecimalFormat myFormat=new DecimalFormat(pattern);
// 将数字进行格式化
String output = myFormat.format(value);
System.out.println(value+" "+pattern+output);
}
// 使用apply方法对数字进行格式化
static public void UseApplyPatternFormat(String pattern,double value){
// 实例化DecimalFormat对象
DecimalFormat myFormat=new DecimalFormat();
myFormat.applyPattern(pattern);
System.out.println(value+" "+pattern+" "+myFormat.format(value));
}
运行结果:
123456.789 ###,###.###123,456.789
123456.789 00000000.####kg00123456.789kg
123.78 000000.000000123.780
0.789 #.###% 78.9%
123456.789 ###.## 123456.79
0.789 0.00‰ 789.00‰
0.234 0.00¤ 0.23¥
2\ \
DecimalFormat myFormat = new DecimalFormat();
myFormat.setGroupingSize(2);
String output=myFormat.format(1234567.89123);
System.out.print(output);