| No. | 方法 | 類型 | 描述 |
| 1 | public static Locale[] getAvailableLocales() | 普通 | 返回所有語言環境的數組 |
| 2 | public static final NumberFormat getInstance() | 普通 | 返回當前默認語言環境的數字格式 |
| 3 | public static NumberFormat getInstance(Locale inLocale) | 普通 | 返回指定語言環境的數字格式 |
| 4 | public static final NumberFormat getCurrencyInstance() | 普通 | 返回當前默認環境的貨幣格式 |
| 5 | public static NumberFormat getCurrencyInstance(Locale inLocale) | 普通 | 返回指定語言環境的數字格式 |
package com.pb.demo1;import java.text.NumberFormat;public class NumberFormatTest { public static void main(String[] args) { NumberFormat nf=NumberFormat.getInstance(); System.out.結果:
格式化后顯示數字:10,000,000格式化后顯示數字:10,000.345
三、DecimalFormatDecimalFormat也是Format的一個子類,主要的作用是用來格式化數字使用,當然,在格式化數字的時候要比直接使用NumberFormat更加方便,因為可以直接指定按用戶自定義的方式進行格式化操作,與SimpleDateFormat類似,如果要想進行自定義格式化操作,則必須指定格式化操作的模板。| No. | 標記 | 位置 | 描述 |
| 1 | 0 | 數字 | 代表阿拉伯數字,每一個0表示一位阿拉伯數字,如果該位不存在則顯示0 |
| 2 | # | 數字 | 代表阿拉伯數字,每一個#表示一位阿拉伯數字,如果該位不存在則不顯示 |
| 3 | . | 數字 | 小數點分隔符或貨幣的小數分隔符 |
| 4 | - | 數字 | 代表負號 |
| 5 | , | 數字 | 分組分隔符 |
| 6 | E | 數字 | 分隔科學計數法中的尾數和指數 |
| 7 | ; | 子模式邊界 | 分隔正數和負數子模式 |
| 8 | % | 前綴或后綴 | 數字乘以100并顯示為百分數 |
| 9 | /u2030 | 前綴或后綴 | 乘以1000并顯示為千分數 |
| 10 | ¤/u00A4 | 前綴或后綴 | 貨幣記號,由貨幣號替換。如果兩個同時出現,則用國際貨幣符號替換。如果出現在某個模式中,則使用貨幣小數分隔符,而不使用小數分隔符。 |
| 11 | , | 前綴或后綴 | 用于在前綴或或后綴中為特殊字符加引號,例如 "'#'#" 將 123 格式化為 "#123"。要創建單引號本身,請連續使用兩個單引號:"# o''clock"。 |
package com.pb.demo1;import java.text.DecimalFormat;public class FormatDemo { public void format(String pattern,double value){ DecimalFormat df=new DecimalFormat(pattern); String str=df.format(value); System.out.println("使用" + pattern+ "/t格式化數字"+value+":/t" + str); } public static void main(String[] args) { FormatDemo demo=new FormatDemo(); demo.format("###,###.###", 111222.34567); demo.format("000,000.000", 11222.34567); demo.format("###,###.###$", 111222.34567); demo.format("000,000.000¥", 11222.34567); demo.format("##.###%", 0.345678); // 使用百分數形式 demo.format("00.###%", 0.0345678); // 使用百分數形式 demo.format("###.###/u2030", 0.345678); // 使用千分數形式 }}新聞熱點
疑難解答