NumberFormat

package json712.study_javaio;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Currency;
import java.util.Locale;
import java.util.ResourceBundle;

public class Test {
	public static void main(String[] args) {
		Test test=new Test();
		test.test();
		//test.testNumberFormat();
		test.testDecimalFormat();
	}

	public void test() {
		double d = 3.1415926;
		double d1 = 100.2;
		long l = 123456789;
		String s = "987,3.1415926";
		 NumberFormat nf = NumberFormat.getInstance();
		 nf.setMaximumFractionDigits(2);
		 nf.setMinimumFractionDigits(2);
		 nf.setMinimumIntegerDigits(2);
		 nf.setMaximumIntegerDigits(2);
		 System.out.println(nf.format(d));//03.14
		 System.out.println(nf.format(d1));//00.12
		DecimalFormat df = new DecimalFormat();
		// 0占位符:显示pattern规定的位数,没有用0补齐
		df.applyPattern("0.00");//3.14 100.20
		System.out.println("with pattern 0.00 :"+df.format(d));
		System.out.println("with pattern 0.00 :"+df.format(d1));
		df.applyPattern("000.0000");//003.1416 100.2000
		System.out.println("with pattern 000.0000 :"+df.format(d));
		System.out.println("with pattern 000.0000 :"+df.format(d1));
		df.applyPattern("000");//003 100
		System.out.println("with pattern 000 :"+df.format(d));
		System.out.println("with pattern 000 :"+df.format(d1));
		// #占位符:显示pattern规定的位数,没有无需补充
		df.applyPattern("##.###");//3.142 100.2
		System.out.println("with pattern ##.### :"+df.format(d));
		System.out.println("with pattern ##.### :"+df.format(d1));
		df.applyPattern("#");//3 100
		System.out.println("with pattern # :"+df.format(d));
		System.out.println("with pattern # :"+df.format(d1));
		// 以百分数形式显示
		df.applyPattern("#.00%");//314.16% 10020.00
		System.out.println("with pattern #.00% :"+df.format(d));
		System.out.println("with pattern #.00% :"+df.format(d1));
		df.applyPattern("#.#%");//314.2% 10020%
		System.out.println("with pattern #.#% :"+df.format(d));
		System.out.println("with pattern #.#% :"+df.format(d1));
		// 以千分数显示,并取三位小数,不够用0补齐
		df.applyPattern("#.000\u2030");//3141.593‰ 100200.000‰
		System.out.println("with pattern #.000\u2030 :"+df.format(d));
		System.out.println("with pattern #.000\u2030 :"+df.format(d1));
		// 添加分组分隔符 、负号
		df.applyPattern("-,###.##");//123,456,789
		System.out.println("with pattern -,###.## :"+df.format(l)); 
		// 嵌入文本
		df.applyPattern("看看-,###.##ok");//看看-123,456,789ok 看看-100.2ok
		System.out.println("with pattern 看看-,###.##ok :"+df.format(l)); 
		// 解析文本成Number
		Number n = null;
		try {
			nf=NumberFormat.getNumberInstance();
			n = nf.parse(s);// 9873.1415926
		} catch (ParseException e) {
			e.printStackTrace();
		}
		System.out.println("parse s:"+String.valueOf(n));
	}

	/**
	 * NumberFormat:public abstract class NumberFormat,是全部数值格式的抽象基类。
	 * 抽象类,获取实例:NumberFormat nf = NumberFormat.getInstance();
	 * 格式化数值:nf.format(number); 解析数值文本:nf.parse(strNumber);
	 * 主要做用体如今国际化格式规范定义,以及获取简单数值格式,具体格式约束经过其子类:DecimalFormat实现
	 */
	public void testNumberFormat() {
		double d = 3.1415926;
		int i = 123456;
		String s = "3.1415926";
		// NumberFormat
		NumberFormat nf = NumberFormat.getNumberInstance();// NumberFormat.getInstance()等效
		// 添加地区国际化语言
		nf.getInstance(Locale.CHINA);// getInstance(Locale.CHINA);
		Locale.setDefault(Locale.CHINA);// 设置JVM虚拟机默认语言格式
		// 得到整数格式
		nf.getIntegerInstance();
		// 得到百分数格式
		nf.getPercentInstance();
		// 得到货币格式
		nf.getCurrencyInstance();
		// 解析给定字符串开头的文本,生成一个数值
		try {
			nf.parse(s);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		// 设置此格式中是否使用分组
		nf.setGroupingUsed(true);
		// 设置数的小数部分所容许的最大位数、最小位数,系统默认为三位小数
		nf.setMaximumFractionDigits(2);
		nf.setMinimumFractionDigits(2);
		// 设置正数部分所容许的最大位数、最小位数
		nf.setMaximumIntegerDigits(2);
		nf.setMinimumIntegerDigits(2);
		// 使用上面定义的nf,格式规范数字d
		String sd = nf.format(d);
	}

	/**
	 * DecimalFormat 是 NumberFormat 的一个具体子类,用于格式化十进制数字 要获取具体语言环境的
	 * NumberFormat(包括默认语言环境),可调用 NumberFormat 的某个工厂方法,如 getInstance()。 一般不直接调用
	 * DecimalFormat 的构造方法,由于 NumberFormat 的工厂方法可能返回不一样于 DecimalFormat 的子类。
	 */
	public void testDecimalFormat() {
		String pattern = "";
		// 使用默认模式和默认语言环境的符号建立一个 DecimalFormat
		DecimalFormat df = new DecimalFormat();
		// 使用给定的模式和默认语言环境的符号建立一个 DecimalFormat
		DecimalFormat df1 = new DecimalFormat(pattern);
		// 经过继承获取
		DecimalFormat df2 = null;
		try {
			df2 = (DecimalFormat) NumberFormat.getInstance();
		} catch (ClassCastException e) {
			System.out.println(e);
		}
		// 为格式化对象添加格式模式
		df.applyPattern(pattern);
		// 使用分组,默认为true,并设置分组大小
		df.setGroupingUsed(true);
		df.setGroupingSize(3);
		// 设置负数前、后缀
		df.setNegativePrefix("-");
		df.setNegativeSuffix("%");
		// 设置正数前、后缀
		df.setPositivePrefix("+");
		df.setPositiveSuffix("%");
		// 使用方法控制小数位数、整数位数、货币格式
		df.setMaximumFractionDigits(2);
		df.setMinimumIntegerDigits(2);
		df.setCurrency(Currency.getInstance(Locale.CHINA));
		// 容许设置整数中小数分隔符的行为。(有小数时始终显示小数分隔符。true时,无小数也显示小数分隔符)
		df.setDecimalSeparatorAlwaysShown(false);
		// 设置小数的舍入模式,默认模式为RoundingMode.HALF_EVEN
		df.setRoundingMode(RoundingMode.HALF_EVEN);
		/*
		 * pattern的形式举例: 一、
		 */
		System.out.println("testDecimalFormat:"+df.format(12456789));
	}

}

运行结果:java

03.14
00.20
with pattern 0.00 :3.14
with pattern 0.00 :100.20
with pattern 000.0000 :003.1416
with pattern 000.0000 :100.2000
with pattern 000 :003
with pattern 000 :100
with pattern ##.### :3.142
with pattern ##.### :100.2
with pattern # :3
with pattern # :100
with pattern #.00% :314.16%
with pattern #.00% :10020.00%
with pattern #.#% :314.2%
with pattern #.#% :10020%
with pattern #.000‰ :3141.593‰
with pattern #.000‰ :100200.000‰
with pattern -,###.## :-123,456,789
with pattern 看看-,###.##ok :看看-123,456,789ok
parse s:9873.1415926
testDecimalFormat:+12,456,789%
相关文章
相关标签/搜索