爲了回答這個問題,我們首先要問,什麼是貨幣字符串組成的呢?
那麼它包括:
- 可選的貨幣符號(如美元,歐元,或$)
- 可選空格(使用
Character.isSpaceChar
或Character.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;
}
我要評論說,這不是一般的可能,如果「地球上任何有效的貨幣」數:例如,目前還不清楚沒有更多的是否「$ 100.00」指的是美國,加拿大,澳大利亞,或其他元。 –
你要求不高,你 – Bohemian