2011-08-19 3 views
2

100,00€ 또는 $100.00 또는 100.00USD (임의의 길이, Symbol과 ISO-Code의 유효한 통화) ... (= like 100.000.000,00 EUR)과 같은 통화가있는 임의의 문자열이 있습니다. 통화가 올바른지 보장은 없다, 그것은 잘못된 기호 또는 문자 수 있습니다 또는 잘못된 위치에서 (후 또는 번호 앞에) 임의 통화 문자열 - 모든 부품을 분리합니까?

무엇을 얻을 수있는 가장 쉬운 방법입니다

  1. 정수 부분
  2. 소수 부분
  3. 통화 (유효한 경우)

내가 NumberFormat/CurrencyFormat 알고 있지만 advanc의 정확한 로케일을 알고 있다면이 클래스는 유용합니다 전자 및 올바르게 서식이 지정된 문자열로만 작동하는 것으로 보인다 ... asw는 통화가 아니라 숫자 만 반환합니다.

대단히 고마워요! 마커스

+1

는 예를 들어, 그것은없이 명확하지 않다 "$ 100.00"는 미국, 캐나다, 호주, 등을 말한다 더 있는지 여부 불화. –

+0

많은 것을 요구하지 않습니다. – Bohemian

답변

6

이 질문에 대한 답을 얻으려면 먼저 통화 문자열이 무엇입니까? 0에서

  • 옵션 공백 (사용 Character.isSpaceChar 또는 Character.isWhitespace)
  • 하나 이상의 숫자 (예 : USD, EUR, 또는 $ 등)

    • 옵션 통화 기호 :

      음이 구성 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

    안녕하세요. 귀하의 답변에 감사드립니다. 내 질문은 주로 자바 클래스 (또는 라이브러리)를 이미 놓친 일이 없도록 shure를 만드는 것이었다. 당신은 나를 위해 특히 수업을 작성하지 않아도 ... 당신의 대답에 감사드립니다! – Markus

    +0

    기지가 정확하고 완벽에 가까움 +1 – mKorbel

    +0

    와우, 정말 고마워. – Markus

    관련 문제