2011-07-14 42 views
0

我嘗試在Android中使用NumberFormatter格式化數字。我使用的代碼波紋管和它完美的作品:DecimalFormat模式問題

NumberFormat formatter = new DecimalFormat("###,###"); 
String myFormattedString = formatter.format(123456); 

但是,當我使用的模式與空間,這樣的:new DecimalFormat("###,# ##");它拋出一個IllegalArgumentException。我已閱讀關於NumberFormatter和DecimalFormatter的文檔,並沒有發現任何關於模式中的空格。任何人都可以解釋爲什麼我不能使用空格或者如何將它們添加爲允許的字符。 在此先感謝!

回答

4

您不能將空格放在數字中間:它不是有效的格式。

如果你看一下the JavaDoc of DecimalFormat,你會看到這一點:

 
Prefix: 
     any Unicode characters except \uFFFE, \uFFFF, and special characters 
Suffix: 
     any Unicode characters except \uFFFE, \uFFFF, and special characters 
Number: Integer Exponentopt Integer . Fraction Exponentopt 

無需複製整個文檔,沒有Number模式的組成部分接受的空間,所以要滿足一個空間在中間將無法正常工作。您只能在前綴或後綴中使用空格。

3

在常規的JDK中,這不會引發異常 - 它僅將格式設置爲123,456

不清楚你的例子中的空間是什麼。您有一個符號的作用,幾個選項:

  • 小數點分隔
  • 組分隔
  • 指數分隔
  • 貨幣小數點分隔符

您可以設置每一種搭配:

DecimalFormatSymbols symbols = formatter.getDecimalFormatSymbols(); 
symbols.setGroupingSeparator(' '); 
formatter.setSymbols(symbols); 
0

我已經完成了我的g通過使用我自己的格式化程序使用標準格式化程序並使用exeptions查找禁止的符號。希望它對別人有用。

public static String getFormattedNumberWithPattern(String aPattern, 
      float aNumber) { 
     String lFormattedNumber = null; 
     String lOriginalPattern = aPattern; 
     try { 
      Hashtable<Integer, String> lIlligalChars = new Hashtable<Integer, String>(); 
      // analyze illegal characters 
      for (int i = aPattern.length() - 1; i >= 0; i--) { 
       char[] lAux = new char[1]; 
       aPattern.getChars(i, i + 1, lAux, 0); 
       try { 
        // if character is illegal, it throws an exception 
        @SuppressWarnings("unused") 
        NumberFormat lNumberFormatter = new DecimalFormat("#" 
          + lAux[0] + "#"); 
       } catch (IllegalArgumentException e) { 
        // add illegal chars and indexes to dictionary 
        lIlligalChars.put(new Integer(i), String.valueOf(lAux[0]));}} 
      Enumeration<String> lChars = lIlligalChars.elements(); 
      while (lChars.hasMoreElements()) { 
       String lIllegalChar = lChars.nextElement(); 
       // remove illegal chars from pattern 
       aPattern = removeChar(aPattern, lIllegalChar.charAt(0)); 
      } 

      // format number using legal pattern 
      NumberFormat lNumberFormatter = new DecimalFormat(aPattern); 
      lFormattedNumber = lNumberFormatter.format(aNumber); 

      int lLenghtDifference = lOriginalPattern.length() 
        - lFormattedNumber.length(); 
      // add illegal chars to formatted string using their indexes 
      Enumeration<Integer> lKeys = lIlligalChars.keys(); 
      while (lKeys.hasMoreElements()) { 
       Integer lIllegalCharIndex = lKeys.nextElement(); 
       int lInsertIndex = lIllegalCharIndex.intValue() 
         - lLenghtDifference; 
       // insert illegal chars into formatted number 
       if (lInsertIndex >= 0 
         || lInsertIndex < lFormattedNumber.length()) { 
        lFormattedNumber = new StringBuffer(lFormattedNumber) 
          .insert(lInsertIndex, 
            lIlligalChars.get(lIllegalCharIndex) 
              .charAt(0)).toString(); 
       } 
      } 
     } catch (Exception e) { 
//   Log.d("info", "formater error:" + e + "mask: " + aPattern 
//     + " number:" + aNumber); 
     } 

     return lFormattedNumber; 
    } 

public static String removeChar(String s, char c) { 
     StringBuffer r = new StringBuffer(s.length()); 
     r.setLength(s.length()); 
     int current = 0; 
     for (int i = 0; i < s.length(); i++) { 
      char cur = s.charAt(i); 
      if (cur != c) 
       r.setCharAt(current++, cur); 
     } 
     r.setLength(current); 
     return r.toString(); 
    }