NumberFormat类简单介绍

来源:互联网 发布:邮件淘宝客 编辑:程序博客网 时间:2024/06/05 09:48

NumberFormat类简单介绍

在官方文档中有这嬷一句介绍Theabstract base class for all number format, This class provides the interfacefor formatting and parsing numbers.用于所有数字格式的抽象基类,这个类提供了格式化和解析数字的接口。即可以对所有的数字进行格式化处理。

使用# getInstance或# getnumberinstance得到默认的数字格式。使用getintegerinstance()得到整数格式,getcurrencyinstance()获得货币的数字格式,并getpercentinstance()为显示百分比得到格式。

setMaximumFractionDigits(int newValue) 设置某个数的小数部分中所允许的最大数字位数

setMinimumFractionDigits (int value) 设置格式化时打印的小数位数的最小值。

format();将其他类型数据转变为String类型

 

private staticString getdata(String data) {

StringdString = null;

NumberFormatnt = NumberFormat.getPercentInstance();// 获取保留两位小数的百分比格式

nt.setMinimumFractionDigits(2);

if (data != null && !"null".equals(data)) {

                  // 设置百分数精确度2即保留两位小数

                  dString = nt.format(Double.parseDouble(data));

                  } else {

                  dString = nt.format(Double.parseDouble("0.0"));

}

         returndString + "  ";

}
0 0
原创粉丝点击