任意货币字符串 - 将所有部分分开?

发布于 2024-11-30 03:27:40 字数 414 浏览 1 评论 0原文

我有任意字符串,其货币如 100,00€$100.00100.00USD (任意长度,地球上任何有效的货币,符号和 ISO -代码)...(=如 100.000.000,00 EUR)。不能保证货币是正确的,它可能是无效的符号或字符或位于错误的位置(数字之后或之前)...

最简单的获取方法是什么:

  1. 整数部分
  2. 小数部分
  3. 货币 (如果有效)

我知道 NumberFormat/CurrencyFormat 但这个类只有在您提前知道确切的语言环境时才有用,并且似乎只适用于格式正确的字符串...asw 只会返回数字,不是货币...

非常感谢! 马库斯

I have arbitrary String with currencies like 100,00€ or $100.00 or 100.00USD (arbitrary lenght, 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 might be an invalid Symbol or Character or at the wrong position (after or before the number)...

What is the easiest way to get:

  1. The integer part
  2. The decimal part
  3. The Currency (if valid)

I know of NumberFormat/CurrencyFormat but this class is only usefull if you know the exact locale in advance and seems to be working only to correctly formatted string... asw well only returns the number, not the currency...

Thank you very much!
Markus

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请止步禁区 2024-12-07 03:27:40

为了帮助回答这个问题,我们首先应该问,货币串由什么组成?

它由以下部分组成:

  • 可选的货币符号(例如 USD、EUR 或 $)
  • 可选的空格(使用 Character.isSpaceCharCharacter.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;
}

To help answer this question we should first ask, what does a currency string consist of?

Well it consists of:

  • An optional currency symbol (such as USD, EUR, or $)
  • Optional white space (use Character.isSpaceChar or Character.isWhitespace)
  • One or more digits from 0 to 9, separated by periods or commas
  • A final period or comma
  • Two digits from 0 to 9
  • If no currency symbol started the string, optional white space and a currency symbol

I will soon create a concrete class for this question, but for now I hope this provides a starting
point for you. Note, however, that some currency symbols such as $ cannot uniquely identify a particular currency without more, as I explained in my comment.

Edit:

Just in case someone else visits this page and encounters the same problem, I've written the code below that answers the question more concretely. 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文