本文介绍了任意货币字符串-将所有部分分开吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有任意货币类型的String,其货币为 100,00€ $ 100.00 100.00 USD (任意长度,地球上任何有效货币,包括符号和ISO代码)...(=像 100.000.000,00 EUR ) 。无法保证货币是正确的,它可能是无效的符号或字符或位置错误(在数字之后或之前)...

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. 整数部分

  2. 小数部分

  3. 货币(如果有效)

我知道 NumberFormat / CurrencyFormat ,但是此类仅在您事先知道确切的语言环境且仅在正确格式化字符串的情况下才有用...因为仅返回数字,而不是货币...

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...

非常感谢!
Markus

Thank you very much!Markus

推荐答案

为帮助回答这个问题,我们首先要问一下,货币字符串由什么组成?

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

它由以下部分组成:


  • 可选的货币符号(例如美元,欧元或$)

  • 可选的空格(使用 Character.isSpaceChar Character.isWhitespace

  • 从0到9的一个或多个数字,用句点或逗号分隔

  • 最后一个句号或逗号

  • 从0到9的两位数字

  • 如果没有货币符号,则字符串,可选的空格和货币符号开始

  • 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 startingpoint for you. Note, however, that some currency symbols such as $ cannot uniquely identify a particular currency without more, as I explained in my comment.

编辑:

以防万一其他人访问此页面并遇到相同的问题,我在下面编写了可更具体地回答该问题的代码。下面的代码在公共领域中。

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

它返回下面定义的CurrencyValue对象。

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

这篇关于任意货币字符串-将所有部分分开吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 10:10