Java民工的鐵皮房

          Consciousness Of Programming - wjywilliam 想飛,總是會飛的......
          posts - 8, comments - 14, trackbacks - 0, articles - 9
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          眀海棠文集之數(shù)據(jù)格式化1.0

          Posted on 2007-04-06 23:56 wjywilliam 閱讀(414) 評論(0)  編輯  收藏 所屬分類: Java

          眀海棠文集之數(shù)據(jù)格式化1.0

           

          java.text 包允許通過與特定語言無關(guān)的方式格式化文本消息、日期和數(shù)值。

          1.        數(shù)據(jù)格式化相關(guān)類介紹

          功能介紹

          java.util.*

          Locale

          表示一個語言和區(qū)域的特定組合

          ResourceBundle

          ListResourceBundle

          PropertyResourceBundle

          獲取本地化資源中(可以表現(xiàn)為類和資源文件)的信息

          Calendar

          GregorianCalendar

          日歷的支持

          TimeZone

          SimpleTimeZone

          時區(qū)的支持

          Currency

          單獨處理和貨幣相關(guān)的操作

          java.text.*

          Format

          NumberFormat

           

          DecimalFormat

          格式化

          格式化數(shù)字、貨幣以及百分數(shù)

          ChoiceFormat

          處理復數(shù)形式

          DateFormat

          SimpleDateFormat

          日期和時間的格式化

          MessageFormat

           

          消息的格式化

          DecimalFormatSymbols DateFormatSymbols

          自定義格式化中的符號集

          FieldPosition

          Format及其子類用來在格式化輸出中標識字段

          Collator

          RuleBasedCollator

          字符串操作

          比較字符串

          CollationElementIterator

          獲得一個字符串中單個字符的枚舉信息

          CollationKey

          優(yōu)化比較性能

          BreakIterator

          獲得文本中的個體信息,比如字符、單詞、句子以及整行等信息

          java.lang.*

          Character

          檢查字符屬性

           

          2.        國際化及 MessageFormat

          MessageFormat 運行開發(fā)者輸出文本中的變量的格式,它主要用于國際化。它是一個強大的類,就像下面的例子展示的那樣:

          String message =

          "Once upon a time ({1,date}, around about {1,time,short}), there " +

          "was a humble developer named Geppetto who slaved for " +

          "{0,number,integer} days with {2,number,percent} complete user " +

          "requirements. ";

          Object[ ] variables =

          new Object[ ] { new Integer(4), new Date( ), new Double(0.21) }

          String output = MessageFormat.format( message, variables );

          System.out.println(output);

          隱藏在信息中的是描述輸出的格式的一種短小的代碼,范例的輸出如下:

          Once upon a time (Nov 3, 2002, around about 1:35 AM), there was a humble developer named Geppetto who slaved for 4 days with 21% complete user requirements.

          如果相同的信息需要被重復輸出但是變量的值不同,那么創(chuàng)建一個 MessageFormat 對象并給出信息。下面是上面的例子的修正版:

          // String output = MessageFormat.format( message, variables );

          // 變?yōu)?/span> :

          MessageFormat formatter = new MessageFormat(message);

          String output = formatter.format(variables);

          除了可以處理日期、時間、數(shù)字和百分數(shù)外, MessageFormat 也可以處理貨幣,運行更多的數(shù)字格式的控制并且允許指定 ChoiceFormat

          MessageFormat  是一個極好的類,它應該經(jīng)常被使用但是現(xiàn)在還沒有。它的最大的缺點是數(shù)據(jù)是被作為變量傳遞而不是一個 Properties 對象。一個簡單的解決辦法是寫一個封裝類,它會預解析字符串為格式化的結(jié)果,將 Properties key 轉(zhuǎn)換為一個數(shù)組索引,順序是 Properties.keys( ) 返回的順序。

          3.       數(shù)值格式化

          3.1. 有關(guān) numberformat

          如果您來自美國,您會在較大的數(shù)值中間放置逗號來表示千和百萬(等等,每三個數(shù)值使用一個逗號)。對于浮點數(shù),您將在整數(shù)部分和小數(shù)部分之間放置小數(shù)點。對于金錢,貨幣符號 $ 放在金額的前面。如果 您從來沒有到過美國以外的地方,可能就不會關(guān)心用英鎊(£)來格式化的英國貨幣,或者用歐元( )來表示的其他歐洲國家的貨幣。

          對于那些我們確實關(guān)心的貨幣,我們可以使用 NumberFormat 及其相關(guān)的類來格式化它們。開發(fā)人員使用 NumberFormat 類來讀取用戶輸入的數(shù)值,并格式化將要顯示給用戶看的輸出。

          Java I/O 里,并沒有所謂的型別,不管是 int long double… 最後都是以 String 輸出,所以如果要讓數(shù)字以特定格式輸出,需透過 Java 提供的兩個類別 java.text.NumberFormat java.text.DecimalFormat 將數(shù)字格式化後再輸出。

          在開始使用 NumberFormat 時,應先用 getInstance 取得 NumberFormat 的實體,范例 12 中的 setMaximumIntegerDigits setMinimumFractionDigits 是用來設(shè)定整數(shù)和小數(shù)的位數(shù),另外還有 setMinimumIntegerDigits setMaximumFractionDigits 也是同樣功能。這些設(shè)定如有沖突, Java 以最後設(shè)定的為準。

          import java.text.*;

          public class myFormat {

            public myFormat() {

              NumberFormat nf = NumberFormat.getInstance();

              double dblNum = Math.PI;

              System.out.println(dblNum);

              nf.setMaximumIntegerDigits(5);

              nf.setMinimumFractionDigits(4);

              System.out.println("PI: " + nf.format(dblNum));

            }

            public static void main(String[] args) {

              myFormat myFormat1 = new myFormat();

            }

          }

          DateFormat 類似, NumberFormat 是一個抽象類。您永遠不會創(chuàng)建它的實例 —— 相反, 您總是使用它的子類。雖然可以通過子類的構(gòu)造函數(shù)直接創(chuàng)建子類,不過 NumberFormat 類提供了一系列 getXXXInstance() 方法,用以獲得不同類型的數(shù)值類的特定地區(qū)版本。這樣的方法共有五個:

          • getCurrencyInstance()
          • getInstance()
          • getIntegerInstance()
          • getNumberInstance()
          • getPercentInstance()

          具體使用哪一個方法取決于您想要顯示的數(shù)值類型(或者想要接受的輸入類型)。每個方法都提供了兩個版本 —— 一個版本適用于當前地區(qū),另一個版本接受一個 Locale 作為參數(shù),以便可能地指定一個不同的地區(qū)。

          使用 NumberFormat 的基本過程是獲得一個實例并使用該實例。挑選恰當?shù)膶嵗拇_需要費一番思量通常 您不希望使用通用的 getInstance 或者 getNumberInstance() 版本因為 您不確切知道您將會得到什么。相反您會使用像 getIntegerInstance() 這樣的方法因為 您希望把某些內(nèi)容顯示為整數(shù)而不需要任何小數(shù)值清單 1 展示了這一點我們在其中把數(shù)值 54321 顯示為適合于美國和德國的格式。

          清單 1. 使用 NumberFormat

          import java.text.*;

          import java.util.*;

          public class IntegerSample {

            public static void main(String args[]) {

              int amount = 54321;

              NumberFormat usFormat =

                NumberFormat.getIntegerInstance(Locale.US);

              System.out.println(usFormat.format(amount));

              NumberFormat germanFormat =

                NumberFormat.getIntegerInstance(Locale.GERMANY);

              System.out.println(germanFormat.format(amount));

            }

          }

          運行該代碼將產(chǎn)生如清單 2 所示的輸出。注意第一種格式(美國)中的逗號分隔符和第二種格式中的點號分隔符。

          清單 2. NumberFormat 輸出

          54,321

          54.321

          雖然 NumberFormat 是一個抽象類,并且您將通過像 getIntegerInstance() 這樣的各種方法來使用它的實例,但是 DecimalFormat 類提供了該類的一個具體版本。 您可以顯式地指定字符模式,用以確定如何顯示正數(shù)、負數(shù)、小數(shù)和指數(shù)。如果不喜歡用于不同地區(qū)的預定義格式,您可以創(chuàng)建自己的格式。(在內(nèi)部,或許 NumberFormat 使用的就是 DecimalFormat 。)。

          3.2. 使用 Currency 進行貨幣計算

          前面提到過的 getCurrency() setCurrency() 方法返回新的 java.util.Currency 類的一個實例。這個類允許訪問不同國家的 ISO 4217 貨幣代碼。雖然自從 getCurrencyInstance() 引入以來您就能配合 NumberFormat 一起使用它,然而除了它們的數(shù)字顯示外, 您永遠不能獲得或顯示某個地區(qū)的貨幣符號。有了 Currency 類,現(xiàn)在很容易就可以做到這一點。

          正如前面提到過的貨幣代碼來自 ISO 4217 。通過傳入某個國家的 Locale 或者貨幣的實際字母代碼, Currency.getInstance() 將返回一個有效的 Currency 對象。 NumberFormat getCurrency() 方法將在創(chuàng)建特定地區(qū)的貨幣實例之后做同樣的事情。 清單 7 顯示了如何獲得貨幣實例,以及如何格式化將要顯示為貨幣的數(shù)值。記住這些轉(zhuǎn)換僅用于顯示。如果需要在貨幣之間轉(zhuǎn)換金額,應該在確定如何顯示值之前進行轉(zhuǎn)換。

          清單 7. 使用 getCurrencyInstance() Currency

           

          import java.text.*;

          import java.util.*;

          import java.awt.*;

          import javax.swing.*;

           

          public class CurrencySample {

            public static void main(String args[]) {

               StringBuffer buffer = new StringBuffer(100);

              Currency dollars = Currency.getInstance("USD");

              Currency pounds = Currency.getInstance(Locale.UK);

              buffer.append("Dollars: ");

              buffer.append(dollars.getSymbol());

              buffer.append("\n");

              buffer.append("Pound Sterling: ");

              buffer.append(pounds.getSymbol());

              buffer.append("\n-----\n");

              double amount = 5000.25;

              NumberFormat usFormat = NumberFormat.getCurrencyInstance(Locale.US);

              buffer.append("Symbol: ");

              buffer.append(usFormat.getCurrency().getSymbol());

              buffer.append("\n");

              buffer.append(usFormat.format(amount));

              buffer.append("\n");

              NumberFormat germanFormat =

                NumberFormat.getCurrencyInstance(Locale.GERMANY);

              buffer.append("Symbol: ");

              buffer.append(germanFormat.getCurrency().getSymbol());

              buffer.append("\n");

              buffer.append(germanFormat.format(amount));

              JFrame frame = new JFrame("Currency");

              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

              JTextArea ta = new JTextArea(buffer.toString());

              JScrollPane pane = new JScrollPane(ta);

              frame.getContentPane().add(pane, BorderLayout.CENTER);

              frame.setSize(200, 200);

              frame.show();

            }

          }             

          遺憾的是,為歐元或者英鎊返回的貨幣符號不是實際的符號,而是三位的貨幣代碼(來自 ISO 4217 )。然而在使用 getCurrencyInstance() 的情況下,實際的符號將會顯示出來,

          3.3. DecimalFormat

          NumberFormat.getInstance() 方法返回 NumberFormat 的一個實例 ( 實際上是 NumberFormat 具體的一個子類,例如 DecimalFormat), 這適合根據(jù)本地設(shè)置格式化一個數(shù)字。你也可以使用非缺省的地區(qū)設(shè)置,例如德國。然后格式化方法根據(jù)特定的地區(qū)規(guī)則格式化數(shù)字。這個程序也可以使用一個簡單的形式:

          NumberFormat.getInstance().format(1234.56)

          但是保存一個格式然后重用更加有效。國際化是格式化數(shù)字時的一個大問題。

          另一個是對格式的有效控制,例如指定小數(shù)部分的位數(shù),下面是解決這個問題的一個簡單例子:

          import java.text.DecimalFormat;

          import java.util.Locale;

          public class DecimalFormat2 {

          public static void main(String args[]) {

          // 得到本地的缺省格式
          DecimalFormat df1 = new DecimalFormat("####.000");

          System.out.println(df1.format(1234.56));

          // 得到德國的格式

          Locale.setDefault(Locale.GERMAN);

          DecimalFormat df2 = new DecimalFormat("####.000");

          System.out.println(df2.format(1234.56));

          }

          }

          在這個例子中設(shè)置了數(shù)字的格式,使用像 "####.000" 的符號。這個模式意味著在小數(shù)點前有四個數(shù)字,如果不夠就空著,小數(shù)點后有三位數(shù)字,不足用 0 補齊。程序的輸出:

          1234.560

          1234,560

          相似的,也可以控制指數(shù)形式的格式,例如:

          import java.text.DecimalFormat;

          public class DecimalFormat3 {

          public static void main(String args[]) {

          DecimalFormat df = new DecimalFormat("0.000E0000");

          System.out.println(df.format(1234.56));

          }

          }

          輸出:

          1.235E0003

          對于百分數(shù):

          import java.text.NumberFormat;

          public class DecimalFormat4 {

          public static void main(String args[]) {

          NumberFormat nf = NumberFormat.getPercentInstance();

          System.out.println(nf.format(0.47));

          }

          }

          輸出:

          47%

          至此,你已經(jīng)看到了格式化數(shù)字的幾個不同的技術(shù)。另一方面,如何讀取并解析包含格式化的數(shù)字的字符串?解析支持包含在 NumberFormat 中。例如:

          import java.util.Locale;

          import java.text.NumberFormat;

          import java.text.ParseException;

          public class DecimalFormat5 {

          public static void main(String args[]) {

          // 本地格式

          NumberFormat nf1 = NumberFormat.getInstance();

          Object obj1 = null;

          // 基于格式的解析

          try {

          obj1 = nf1.parse("1234,56");

          }

          catch (ParseException e1) {

          System.err.println(e1);

          }

          System.out.println(obj1);

          // 德國格式

          NumberFormat nf2 =NumberFormat.getInstance(Locale.GERMAN);

          Object obj2 = null;

          // 基于格式的解析

          try {

          obj2 = nf2.parse("1234,56");

          }

          catch (ParseException e2) {

          System.err.println(e2);

          }

          System.out.println(obj2);

          }

          }

          這個例子分兩部分,都是解析一個字符串: "1234,56" 。第一部分使用本地格式解析,第二部分使用德國格式解析。當程序在美國運行,結(jié)果是:

          123456

          1234.56

          換句話說, "1234,56" 在美國被認為是一個巨大的整數(shù) "123456" 而在德國被認為是一個小數(shù) "1234.56"

          3.4. DecimalFormat NumberFormat 的聯(lián)系

          在上面的例子中, DecimalFormat NumberFormat 都被使用了。 DecimalFormat 常用于獲得很好的格式控制,而 NumberFormat 常用于指定不同于本地的地區(qū)。如何結(jié)合兩個類呢?

          答案圍繞著這樣的事實: DecimalFormat NumberFormat 的一個子類 , 其實例被指定為特定的地區(qū)。因此,你可以使用 NumberFormat.getInstance 指定一個地區(qū),然后將結(jié)構(gòu)強制轉(zhuǎn)換為一個 DecimalFormat 對象。文檔中提到這個技術(shù)可以在大多情況下適用,但是你需要用 try/catch 塊包圍強制轉(zhuǎn)換以防轉(zhuǎn)換不能正常工作 ( 大概在非常不明顯得情況下使用一個奇異的地區(qū) ) 。下面是一個這樣的例子:

          import java.text.DecimalFormat;

          import java.text.NumberFormat;

          import java.util.Locale;

          public class DecimalFormat6 {

          public static void main(String args[]) {

          DecimalFormat df = null;

          // 得到一個 NumberFormat 對象并

          // 強制轉(zhuǎn)換為一個 DecimalFormat 對象

          try {

          df = (DecimalFormat)NumberFormat.getInstance(Locale.GERMAN);

          }

          catch (ClassCastException e) {

          System.err.println(e);

          }

          // 設(shè)置格式模式

          df.applyPattern("####.00000");

          // format a number

          System.out.println(df.format(1234.56));

          }

          }

          getInstance() 方法獲得格式,然后調(diào)用 applyPattern() 方法設(shè)置格式模式,輸出:

          1234,56000

          如果你不關(guān)心國際化,可以直接使用 DecimalFormat

          其中v 為未處理的double,scale為需求精度,返回需要小數(shù)位數(shù)的double

          public static double round(double v,int scale){

                  if(scale<0){

                      throw new IllegalArgumentException(

                          "The scale must be a positive integer or zero");

                  }

                  BigDecimal b = new BigDecimal(Double.toString(v));

                  BigDecimal one = new BigDecimal("1");

                  return b.divide(one,scale,BigDecimal.ROUND_HALF_UP).doubleValue();

              }


           
          package com.minght.sys.util;

          import java.text.*;
          import java.util.*;
          import java.math.*;

          /**
           * <p>Title: 格式化:開源,開放</p>
           * <p>Description: opeansource</p>
           * <p>Copyright: Copyright (c) 2004</p>
           * <p>Company: 眀海棠</p>
           * @author HaiTang Ming
           * @version 1.0
           */
          public class ObjectFormat {
            public ObjectFormat() {
            }

            /**
             * 將給定的數(shù)字按給定的形式輸出
             * @param d double
             * @param pattern String
             *       #:表示有數(shù)字則輸出數(shù)字,沒有則空,如果輸出位數(shù)多于#的位數(shù),則超長輸入
             *       0:有數(shù)字則輸出數(shù)字,沒有補0
             *          對于小數(shù),有幾個#或0,就保留幾位的小數(shù);
             *       例如:  "###.00" -->表示輸出的數(shù)值保留兩位小數(shù),不足兩位的補0,多于兩位的四舍五入
             *              "###.0#" -->表示輸出的數(shù)值可以保留一位或兩位小數(shù);整數(shù)顯示為有一位小數(shù),一位或兩位小數(shù)的按原樣顯示,多于兩位的四舍五入;
             *              "###" --->表示為整數(shù),小數(shù)部分四舍五入
             *              ".###" -->12.234顯示為.234
             *              "#,###.0#"  -->表示整數(shù)每隔3位加一個",";
             * @param l Locale
             * @return String
             */
            public static String formatNumber(double d,String pattern,Locale l){
              String s = "";
              try{
                DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(l);
                nf.applyPattern(pattern);
                s = nf.format(d);

              }catch(Exception e){
                e.printStackTrace();
                Debug.println(" formatNumber is error!");
              }
              return s;

            }

            /**
             * 按缺省的區(qū)域輸出數(shù)字形式
             * @param d double
             * @param pattern String
             * @return String
             */
            public static String formatNumber(double d,String pattern){
              return formatNumber(d,pattern,Locale.getDefault());
            }

            /**
             * 格式化貨幣
             * @param d double
             * @param pattern String
             *        "\u00A4#,###.00" :顯示為 ¥1,234,234.10
             * @param l Locale
             * @return String
             */
            public static String formatCurrency(double d,String pattern,Locale l){
              String s = "";
              try{
                DecimalFormat nf = (DecimalFormat) NumberFormat.getCurrencyInstance(l);
                nf.applyPattern(pattern);
                s = nf.format(d);

              }catch(Exception e){
                e.printStackTrace();
                Debug.println(" formatNumber is error!");
              }
              return s;

            }
            /**
             * 使用默認區(qū)域的指定方式顯示貨幣
             * @param d double
             * @param pattern String
             * @return String
             */
            public static String formatCurrency(double d,String pattern){
              return formatCurrency(d,pattern,Locale.getDefault());
            }

            /**
             * 使用默認方式顯示貨幣:
             *     例如:¥12,345.46  默認保留2位小數(shù),四舍五入
             * @param d double
             * @return String
             */
            public static String formatCurrency(double d){
              String s = "";
             try{
               DecimalFormat nf = (DecimalFormat) NumberFormat.getCurrencyInstance();
               s = nf.format(d);

             }catch(Exception e){
               e.printStackTrace();
               Debug.println(" formatNumber is error!");
             }
             return s;

            }

            /**
             * 按指定區(qū)域格式化百分數(shù)
             * @param d
             * @param pattern  :"##,##.000%"-->不要忘記“%”
             * @param l
             * @return
             */
            public static String formatPercent(double d,String pattern,Locale l){
              String s = "";
              try{
                DecimalFormat df = (DecimalFormat)NumberFormat.getPercentInstance(l);
                df.applyPattern(pattern);
                s = df.format(d);
              }catch(Exception e){
                Debug.print(e,"formatPercent is error!");
              }
              return s;
            }
            /**
             * 使用默認區(qū)域格式化百分數(shù)
             * @param d
             * @param pattern
             * @return
             */
           public static String formatPercent(double d,String pattern){
             return formatPercent(d,pattern,Locale.getDefault());
           }

           /**
            * 格式化百分數(shù)
            * @param d
            * @return
            */
           public static String formatPercent(double d){
             String s = "";
             try{
               DecimalFormat df = (DecimalFormat)NumberFormat.getPercentInstance();
               s = df.format(d);
             }catch(Exception e){
               Debug.print(e,"formatPercent is error!");
             }
             return s;
           }

            /**
             * 輸出數(shù)字的格式,如:1,234,567.89
             * @param bd BigDecimal 要格式華的數(shù)字
             * @param format String 格式 "###,##0"
             * @return String
             */
            public static String numberFormat(BigDecimal bd, String format) {

              if (bd == null || "0".equals(bd.toString())) {
                return "";
              }

              DecimalFormat bf = new DecimalFormat(format);
              return bf.format(bd);
            }


            public static void main(String[] args) {
              String s = formatCurrency(11123.89343,"$##,##.000");
              System.out.println(s);
            }

          }

          轉(zhuǎn)載原作者......


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 乐至县| 富源县| 上饶市| 潮安县| 河间市| 浦东新区| 阿拉善左旗| 平度市| 沂水县| 田阳县| 岳阳县| 农安县| 贵阳市| 六枝特区| 甘孜县| 盱眙县| 常山县| 辛集市| 百色市| 鄂伦春自治旗| 阿尔山市| 溧水县| 宁明县| 甘南县| 老河口市| 兴隆县| 辽中县| 准格尔旗| 扶沟县| 安乡县| 洛阳市| 那坡县| 沅陵县| 澄迈县| 梧州市| 资源县| 阜宁县| 黄大仙区| 商南县| 黔东| 织金县|