2011-08-19 1 views
2

100,00€または$100.00または100.00USD(任意の長さ、SymbolとISOコードの両方の有効な通貨)...(= 100.000.000,00 EUR)のような通貨の任意の文字列があります。任意の通貨文字列 - すべてのパーツを分割しますか?

    :通貨が正しい、それは無効な記号や文字または(番号の後または前に)間違った位置にあるかもしれないという保証は取得するための最も簡単な方法は何ですか

    ...ありません

  1. 整数部
  2. 小数部
  3. 通貨(有効な場合)

私はNumberFormat/CurrencyFormatを知っていますが、advancの中に正確なロケールを知っていれば、このクラスにのみ便利ですeと正しくフォーマットされた文字列にのみ働いているようだ... aswは、通貨ではなく、数値を返します。

ありがとうございました! Markus

+1

私は「地球上の任意の有効な通貨」場合がカウントされ、これは一般的に不可能であることをコメントする必要があります。たとえば、それは「$ 100.00」、米国、カナダ、オーストラリア、またはその他を参照するかどうかよりなしでは不明ですドル。 –

+0

あなたは多くを求めていません。 – Bohemian

答えて

6

この質問に答えるには、まず通貨文字列の構成を教えてください。 0から

  • オプションのホワイトスペース(使用Character.isSpaceCharまたはCharacter.isWhitespace
  • つ以上の数字(例えばUSD、EUR、または$など)

    • オプションの通貨記号:

      は、まあそれはで構成されてい9に、

    • 2桁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

    関連する問題