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%
相關文章
相關標籤/搜索