Android / Java - 将双倍价格转换为String,并给出货币参数

时间:2014-05-20 09:52:56

标签: java android string formatting currency

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

如何将double价格转换为String,并提供Euro[€] / Dollar[$]种货币?

这是我的代码:

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

当我使用作为货币parameter时,我会在IllegalArgumentException获得DecimalFormatter formatter = new DecimalFormatter("€##.###,##");。有谁知道我的DecimalFormatter prefix出了什么问题?

当我从IllegalArgumentException移除$时,我也收到Formatter prefix

提前感谢您的回复。

编辑1(一些例子):

doubleToPrice(4.23, "€"); should return "€ 4,23"
doubleToPrice(7.3, "€"); should return "€ 7,30"
doubleToPrice(9, "€"); should return "€ 9,-"
doubleToPrice(12345678.9, "€"); should return "€ 12.345.678,90"
doubleToPrice(0.39, "€"); should return "€ 0,39"
doubleToPrice(0.06, "€"); should return "€ 0,06"

doubleToPrice(4.23, "$"); should return "$ 4.23"
doubleToPrice(7.3, "$"); should return "$ 7.30"
doubleToPrice(9, "$"); should return "$ 9.-"
doubleToPrice(12345678.9, "$"); should return "$ 12,345,678.90"
doubleToPrice(0.39, "$"); should return "$ 0.39"
doubleToPrice(0.06, "$"); should return "$ 0.06"

编辑2 /解决方案:

接受Hirak的回答,因为他在编辑后涵盖了我想要的大部分内容。不过,以下是使用“hacks”完成的代码,用于将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;
}

1 个答案:

答案 0 :(得分:2)

这是你想要的吗? (请注意,即使我将DOT作为小数分隔符,在输出中,您将获得COMMA作为十进制分隔符,因为Java会根据区域设置在运行时决定。)

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