Java.格式化输出

NumberFormat 类

NumberFormat 表示数字的格式化类,NumberFormat 类的常用方法

tring format(double number) 根据本对象的模式number将格式化为字符串,并返回该字符串对象。
static NumberFormat getCurrencyinstance() 返回代表本地化的当前货币格式的NumberFormat 对象。
static NumberFormat getPerentinstance() 返回代表本地化的当前百分格式的NumberFormat 对象。

DecimalFormat

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

DecimalFormat(String pattern) 构造方法:创建一个据有指定模式(pattern)的DecimalFormat新对象。
void applypattern(String pattern) 在DecimalFormat对象上应用指定的模式。
Strimg format(double number) 根据当前模式将number格式化为字符串,并返回该字符串对象。

猜你喜欢

转载自www.cnblogs.com/H97042/p/10423622.html
今日推荐