2011-08-19 42 views
2

我有像100,00€$100.00100.00USD貨幣任意字符串(任意lenght,地球上的兩個標誌和ISO-代碼中的任何有效的貨幣)...(=像100.000.000,00 EUR)。有沒有保證貨幣是正確的,它可能是一個無效的字符或符號,或在錯誤的位置(後或數前)...任意貨幣字符串 - 將所有零件分開嗎?

什麼是讓最簡單的方法:

  1. 整數部分
  2. 小數部分
  3. 的貨幣(若有效)

我知道NumberFormat/CurrencyFormat但這個類是隻有有用的,如果你知道在advanc確切的語言環境e和似乎是工作才正確格式化字符串... ASW以及只返回數量,而不是貨幣...

非常感謝您! 馬庫斯

+1

我要評論說,這不是一般的可能,如果「地球上任何有效的貨幣」數:例如,目前還不清楚沒有更多的是否「$ 100.00」指的是美國,加拿大,澳大利亞,或其他元。 –

+0

你要求不高,你 – Bohemian

回答

6

爲了回答這個問題,我們首先要問,什麼是貨幣字符串組成的呢?

那麼它包括:

  • 可選的貨幣符號(如美元,歐元,或$)
  • 可選空格(使用Character.isSpaceCharCharacter.isWhitespace
  • 一個或多個數字從0至圖9,由句點或逗號
  • 最後一個週期或逗號分隔
  • 兩位數從0到9
  • 如果沒有貨幣符號開始的字符串,可選的空格和貨幣符號

我會很快創造了這個問題的具體類,但現在我希望這提供了一個出發點 你。但是請注意,某些貨幣符號如$不能唯一地識別某一特定貨幣沒有更多的,正如我在評論解釋。

編輯:

萬一別人訪問此頁並遇到了同樣的問題,我已經寫了下面的代碼,更具體地回答了這個問題。下面的代碼是在公共領域。

/** 
* Parses a string that represents an amount of money. 
* @param s A string to be parsed 
* @return A currency value containing the currency, 
* integer part, and decimal part. 
*/ 
public static CurrencyValue parseCurrency(String s){ 
    if(s==null || s.length()==0) 
     throw new NumberFormatException("String is null or empty"); 
    int i=0; 
    int currencyLength=0; 
    String currency=""; 
    String decimalPart=""; 
    String integerPart=""; 
    while(i<s.length()){ 
     char c=s.charAt(i); 
     if(Character.isWhitespace(c) || (c>='0' && c<='9')) 
      break; 
     currencyLength++; 
     i++; 
    } 
    if(currencyLength>0){ 
     currency=s.substring(0,currencyLength); 
    } 
    // Skip whitespace 
    while(i<s.length()){ 
     char c=s.charAt(i); 
     if(!Character.isWhitespace(c)) 
      break; 
     i++; 
    } 
    // Parse number 
    int numberStart=i; 
    int numberLength=0; 
    int digits=0; 
    //char lastSep=' '; 
    while(i<s.length()){ 
     char c=s.charAt(i); 
     if(!((c>='0' && c<='9') || c=='.' || c==',')) 
      break; 
     numberLength++; 
     if((c>='0' && c<='9')) 
      digits++; 
     i++; 
    } 
    if(digits==0) 
     throw new NumberFormatException("No number"); 
    // Get the decimal part, up to 2 digits 
    for(int j=numberLength-1;j>=numberLength-3 && j>=0;j--){ 
     char c=s.charAt(numberStart+j); 
     if(c=='.' || c==','){ 
      //lastSep=c; 
      int nsIndex=numberStart+j+1; 
      int nsLength=numberLength-1-j; 
      decimalPart=s.substring(nsIndex,nsIndex+nsLength); 
      numberLength=j; 
      break; 
     } 
    } 
    // Get the integer part 
    StringBuilder sb=new StringBuilder(); 
    for(int j=0;j<numberLength;j++){ 
     char c=s.charAt(numberStart+j); 
     if((c>='0' && c<='9')) 
      sb.append(c); 
    } 
    integerPart=sb.toString(); 
    if(currencyLength==0){ 
     // Skip whitespace 
     while(i<s.length()){ 
      char c=s.charAt(i); 
      if(!Character.isWhitespace(c)) 
       break; 
      i++; 
     } 
     int currencyStart=i; 
     // Read currency 
     while(i<s.length()){ 
      char c=s.charAt(i); 
      if(Character.isWhitespace(c) || (c>='0' && c<='9')) 
       break; 
      currencyLength++; 
      i++; 
     } 
     if(currencyLength>0){ 
      currency=s.substring(currencyStart, 
        currencyStart+currencyLength); 
     } 
    } 
    if(i!=s.length()) 
     throw new NumberFormatException("Invalid currency string"); 
    CurrencyValue cv=new CurrencyValue(); 
    cv.setCurrency(currency); 
    cv.setDecimalPart(decimalPart); 
    cv.setIntegerPart(integerPart); 
    return cv; 
} 

它返回一個下面定義的CurrencyValue對象。

public class CurrencyValue { 
@Override 
public String toString() { 
    return "CurrencyValue [integerPart=" + integerPart + ", decimalPart=" 
      + decimalPart + ", currency=" + currency + "]"; 
} 
String integerPart; 
/** 
* Gets the integer part of the value without separators. 
* @return 
*/ 
public String getIntegerPart() { 
    return integerPart; 
} 
public void setIntegerPart(String integerPart) { 
    this.integerPart = integerPart; 
} 
/** 
* Gets the decimal part of the value without separators. 
* @return 
*/ 
public String getDecimalPart() { 
    return decimalPart; 
} 
public void setDecimalPart(String decimalPart) { 
    this.decimalPart = decimalPart; 
} 
/** 
* Gets the currency symbol. 
* @return 
*/ 
public String getCurrency() { 
    return currency; 
} 
public void setCurrency(String currency) { 
    this.currency = currency; 
} 
String decimalPart; 
String currency; 
} 
+0

你好彼得。感謝您的回答。我的問題主要是,使舒爾,我沒有錯過任何Java類(或庫)已經做到這一點。你不必爲我特別寫一堂課......謝謝你的回答! – Markus

+0

基數是正確的,非常接近完美+1 – mKorbel

+0

哇,非常感謝。 – Markus