A string of arbitrary currency. Separate all the parts?

I have an arbitrary String with currencies such as 100,00€ or $100.00 or 100.00USD (arbitrary length, any valid currency on earth, both Symbol and ISO-code) ... (= like 100.000.000,00 EUR ) . There is no guarantee that the currencies are correct, it may be the wrong character or symbol or in the wrong position (after or before the number) ...

What is the easiest way to get:

  • Whole part
  • Decimal part
  • Currency (if valid)

I know NumberFormat/CurrencyFormat , but this class is only useful if you know the exact locale in advance and seem to work only with a correctly formatted string ... asw well returns only a number, not a currency ...

Many thanks! Marcus

+2
source share
1 answer

To answer this question, we must first ask what the currency line consists of.

Well, it consists of:

  • Additional currency symbol (e.g. USD, EUR or $)
  • Extra free space (use Character.isSpaceChar or Character.isWhitespace )
  • One or more digits from 0 to 9, separated by periods or commas
  • Last period or comma
  • Two digits from 0 to 9
  • If a currency symbol does not start a line, an optional space and a currency symbol

I will create a concrete class for this question soon, but for now I hope this gives a start for you. Please note, however, that some currency symbols, such as $ , cannot uniquely identify a specific currency, no more, as I explained in my comment.

Edit:

Just in case, someone else visits this page and faces the same problem, I wrote the code below, which more specifically answers the question. The code below is in the public domain.

 /** * 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; } 

It returns a CurrencyValue object defined below.

 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; } 
+6
source

All Articles