为了回答这个问题,我们首先要问,什么是货币字符串组成的呢?
那么它包括:
- 可选的货币符号(如美元,欧元,或$)
- 可选空格(使用
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