DecimalFormat类也是Format的一个子类,主要作用是格式化数字。

在格式化数字的时候比直接使用NumberFormat更加方便,因为可以直接指定按用户自定义的方式进行格式化操作,与SimpleDateFormat类似,如果要进行自定义格式化操作,则必须指定格式化操作的模板。

DecimalFormat类-LMLPHP

//=================================================
// File Name : DecimalFormat_demo
//------------------------------------------------------------------------------
// Author : Common import java.text.DecimalFormat; //类名:Contact
//属性:
//方法:
class FormatDemo{
public void format1(String pattern,double value){
DecimalFormat df = null; //声明一个DecimalFormat对象
df = new DecimalFormat(pattern); //实例化对象
String str = df.format(value); //格式化数字
System.out.println("使用"+pattern+"格式化数字"+value+":"+str);
}
} public class DecimalFormat_demo { public static void main(String[] args) {
// TODO 自动生成的方法存根
FormatDemo demo = new FormatDemo();
demo.format1("###,###.###", 111222.34567);
demo.format1("000,000.000", 11222.34567);
demo.format1("###,###.###¥", 111222.34567);
demo.format1("##.###%", 0.34567); //使用百分数形式
demo.format1("00.###%", 0.034567); //使用百分数形式
demo.format1("###.###\u2030", 0.34567); //使用千分数形式
} }
04-24 05:16