《数字格式化》NumberFormat类的使用

[size=large]实例1:默认数据格式化[/size]

package com.meixin.classuse;

import java.text.NumberFormat;

public class DefaultNumberFormat
{
public static void defaultNumberFormat()
{

int i = 123456;

double x = 882323.23523;

double p = 0.528;

double c = 52.83;

System.out.println("--------------------------------------");

//实例化NumberFormat需要使用NumberFormat的getInstance()方法
NumberFormat nf = NumberFormat.getInstance();
//默认格式化int类型,结果已分节
//结果:Integer 123456 is displayed as 123,456
System.out.println("Integer " + i + " is displayed as " + nf.format(i));
//默认格式化double类型,结果保留三位小数,已分节
//结果:Double 882323.23523 is displayed as 882,323.235
System.out.println("Double " + x + " is displayed as " + nf.format(x));

System.out.println("--------------------------------------");

//实例化为整型数据格式化
NumberFormat nfInt = NumberFormat.getIntegerInstance();
//默认,结果:Integer 123456 is displayed as 123,456
System.out.println("Integer " + i + " is displayed as "
+ nfInt.format(i));

System.out.println("--------------------------------------");

//实例化为数据格式化
NumberFormat nfNumber = NumberFormat.getNumberInstance();
//结果:Double 882323.23523 is displayed as 882,323.235
System.out.println("Double " + x + " is displayed as "
+ nfNumber.format(x));

System.out.println("--------------------------------------");

//百分数格式化
NumberFormat nfPercent = NumberFormat.getPercentInstance();
//结果已四舍五入
//结果:Percent 0.528 is displayed as 53%
System.out.println("Percent " + p + " is displayed as "
+ nfPercent.format(p));

System.out.println("--------------------------------------");

//货币型格式化
NumberFormat nfCurrency = NumberFormat.getCurrencyInstance();
//结果:Currency 52.83 is displayed as ¥52.83
System.out.println("Currency " + c + " is displayed as "
+ nfCurrency.format(c));
}

public static void main(String[] args)
{
defaultNumberFormat();
System.out.println();
}

}


[size=large]实例2:自定义数据格式化[/size]


package com.meixin.classuse;

import java.text.DecimalFormat;

public class CustomNumberFormat
{

public static void customNumberFormat()
{

double x = 100000.0 / 3;

// default output is 33333.333333333336
System.out.println("default output is " + x);
// 33,333.33
System.out.println(new DecimalFormat("###,###.##").format(x));
// 33333.33
System.out.println(new DecimalFormat("####.##").format(x));
// 33333.33
System.out.println(new DecimalFormat("####.00").format(x));
// 33333.33
System.out.println(new DecimalFormat("####.0#").format(x));
// 0033333.33
System.out.println(new DecimalFormat("0000000.##").format(x));
// $33,333.33
System.out.println(new DecimalFormat("$###,###.##").format(x));
// 3.333E4
System.out.println(new DecimalFormat("0.###E0").format(x));
// 3333333.33%
System.out.println(new DecimalFormat("00.##%").format(x));

System.out.println("--------------------------------------");

double y = 23232323.0012;
//default output is 2.32323230012E7
System.out.println("default output is " + y);
//23,232,323
System.out.println(new DecimalFormat("###,###.##").format(y));
//23232323
System.out.println(new DecimalFormat("####.##").format(y));
//23232323.00
System.out.println(new DecimalFormat("####.00").format(y));
//23232323.0
System.out.println(new DecimalFormat("####.0#").format(y));
//0023232323
System.out.println(new DecimalFormat("0000000000.##").format(y));
//$23,232,323
System.out.println(new DecimalFormat("$###,###.##").format(y));
//2.323E7
System.out.println(new DecimalFormat("0.###E0").format(y));
//2323232300.12%
System.out.println(new DecimalFormat("00.##%").format(y));
}

public static void main(String[] args)
{
customNumberFormat();
System.out.println();
}

}

你可能感兴趣的:(类的使用)