2011-08-19 45 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