Android/Java-将double price转换为字符串,并提供货币参数

Android/Java-将double price转换为字符串,并提供货币参数,java,android,string,formatting,currency,Java,Android,String,Formatting,Currency,我知道它已经被问了很多次了,我确实在这个问题上获得了很多Google&stackoverflow的结果,但是我试过的那一个不起作用,并且给出了一个IllegalArgumentExceptions 如何将双倍价格转换为字符串,并提供欧元[€]/美元[$]货币? 这是我的代码: // Convert Price-Double to String public static String doubleToPrice(double price, String currency){ // Cre

我知道它已经被问了很多次了,我确实在这个问题上获得了很多Google&stackoverflow的结果,但是我试过的那一个不起作用,并且给出了一个
IllegalArgumentExceptions

如何将
双倍
价格转换为
字符串
,并提供
欧元[€]
/
美元[$]
货币?

这是我的代码:

// Convert Price-Double to String
public static String doubleToPrice(double price, String currency){
    // Create DecimalFormat based on Currency
    // Default (Euro)
    // Use "€##.###,##"
    DecimalFormat formatter = new DecimalFormat("€##.###,##");
    // currency parameter given
    if(currency != null && !currency.trim().equals(""))
        // Dollar
        // Use "$##,###.##"
        if(currency.trim().equals("$"))
            formatter = new DecimalFormat("$##,###.##");
    // no currency parameter given: use Config.CURRENCY instead
    else
        // Dollar
        // Use "$##,###.##"
        if(compare(currency, Config.CURRENCY))
            formatter = new DecimalFormat("$##,###.##");

    // Format the string
    String priceString = formatter.format(price);

    // Add a space between the currency and the price value
    priceString = priceString.substring(0, 1) + " " + priceString.substring(1, priceString.length());

    // Replace last two "00" digits for "-"
    if(priceString.endsWith("00")){
        int i = priceString.lastIndexOf("00");
        priceString = new StringBuilder(priceString).replace(i, i+2, "-").toString();
    }

    return priceString;
}
当我使用
作为currency
参数时,我在
DecimalFormatter formatter=new DecimalFormatter(“€#####,###”)得到一个
非法argumentexception
。有人知道我的
分码格式前缀有什么问题吗

当我从
格式化程序前缀
中删除
$
时,我还会收到一个
非法argumentexception

提前感谢您的回复

编辑1(一些示例):

编辑2/解决方案:

接受了希拉克的回答,因为他涵盖了我编辑后想要的大部分内容。尽管如此,这里仍然是完成的代码,带有将
00
转换为
-
的“hacks”

// Convert Price-Double to String
public static String doubleToPrice(double price, char currency){
    // Define currency to be used
    char curr = Config.CURRENCY;
    if(currency == '€' || currency == '$')
        curr = currency;

    // Format the string using a DecimalFormat
    Locale locale = new Locale("de", "DE");
    if(curr == '$')
        locale = new Locale("en", "US");
    DecimalFormatSymbols sym = new DecimalFormatSymbols(locale);
    sym.setGroupingSeparator('.');
    if(curr == '$')
        sym.setGroupingSeparator(',');
    DecimalFormat formatter = (DecimalFormat)NumberFormat.getNumberInstance(locale);
    formatter.applyPattern(curr + "##,##0.00");
    formatter.setDecimalFormatSymbols(sym);

    String returnString = formatter.format(price);

    // Replace "00" after the comma with "-"
    if(returnString.endsWith("00")){
        int i = returnString.lastIndexOf("00");
        returnString = new StringBuilder(returnString).replace(i, i+2, "-").toString();
    }

    // Add space between currency-symbol and price
    returnString = returnString.substring(0, 1) + " " + returnString.substring(1, returnString.length());

    Log.i("PRICE FORMATTING", "double that goes in [" + price + "]; string that comes out [" + returnString + "]");

    return returnString;
}
这是你想要的吗?(注意,尽管我将点作为十进制分隔符,但在输出中,您将得到逗号作为十进制分隔符,因为Java在运行时根据区域设置进行决定。)


为什么不在strings.xml中创建一个类似“%1$s%2$s”的资源,然后在您的方法中:public static String doubleToPrice(double price,String currency){return getResource().getString(R.String.price,price+“”,currency)}@emipana我曾经这样做过。但是这里的问题是分隔符..抱歉,在strings.xml中放置“new DecimalFormat”(###,#####.######.####.###),format(price)“创建“$%1$,.2f in strings.xml”并按照Emipana你的答案看起来有点不完整。例如,它没有格式7.3到7,30;基于给定的货币参数,fr\u fr和en\u US格式之间也没有区别。我已经对我的主要帖子进行了编辑,以便更清楚地了解我想要实现的目标。我已经对我的代码进行了更改。这应该满足您的所有需要,但是“/”应该返回“9-“目前我想不出一种方法来实现这个模式,除非我们做一些自定义黑客……谢谢,在做了一些更改后,包括一些自定义黑客将结尾
00
转换为
-
,你回答了这个问题。将我完整的解决方案代码添加到主帖子中,并接受您的正确答案。
// Convert Price-Double to String
public static String doubleToPrice(double price, char currency){
    // Define currency to be used
    char curr = Config.CURRENCY;
    if(currency == '€' || currency == '$')
        curr = currency;

    // Format the string using a DecimalFormat
    Locale locale = new Locale("de", "DE");
    if(curr == '$')
        locale = new Locale("en", "US");
    DecimalFormatSymbols sym = new DecimalFormatSymbols(locale);
    sym.setGroupingSeparator('.');
    if(curr == '$')
        sym.setGroupingSeparator(',');
    DecimalFormat formatter = (DecimalFormat)NumberFormat.getNumberInstance(locale);
    formatter.applyPattern(curr + "##,##0.00");
    formatter.setDecimalFormatSymbols(sym);

    String returnString = formatter.format(price);

    // Replace "00" after the comma with "-"
    if(returnString.endsWith("00")){
        int i = returnString.lastIndexOf("00");
        returnString = new StringBuilder(returnString).replace(i, i+2, "-").toString();
    }

    // Add space between currency-symbol and price
    returnString = returnString.substring(0, 1) + " " + returnString.substring(1, returnString.length());

    Log.i("PRICE FORMATTING", "double that goes in [" + price + "]; string that comes out [" + returnString + "]");

    return returnString;
}
   static String doubleToPrice(double dbl,char currency) {
    Locale locale =null;
    if(currency=='€') {
    locale  = new Locale("fr", "FR");
    }else {
        locale  = new Locale("en", "EN");
    }//Add locales as per need.
    DecimalFormatSymbols sym = new DecimalFormatSymbols(locale);
    sym.setGroupingSeparator('.');
    DecimalFormat decimalFormat = (DecimalFormat)
            NumberFormat.getNumberInstance(locale);
    decimalFormat.applyPattern(currency+"##,###.00");
    decimalFormat.setDecimalFormatSymbols(sym);
    return decimalFormat.format(dbl);
}