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

标签 java android string formatting currency

我知道这个问题已经被问过很多次了,而且我确实在这个问题上获得了很多 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;
}

当我使用 作为货币参数时,我在 DecimalFormatter formatter = new DecimalFormatter("处收到 IllegalArgumentException €##.###,##");。有谁知道我的 DecimalFormatter 前缀 有什么问题吗?

当我从 Formatter 前缀 中删除 $ 时,我还会收到 IllegalArgumentException 错误。

提前感谢您的回复。

编辑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 的回答,因为他在编辑后涵盖了我想要的大部分内容。尽管如此,这里还是完成的代码,其中包含将 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;
}

最佳答案

这是你想要的吗? (请注意,即使我将 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);
}

关于java - Android/Java - 将双倍价格转换为字符串,并给出货币参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23756488/

相关文章:

java - 如何连接远程sql server数据库?

java - Wiremock - 从 URL 中获取字符串,修改它并将其作为正文的一部分返回

android - 从联系人列表启动应用程序

android - 哪里可以下载谷歌绿色机器人的矢量文件?

javascript - chop 字符串 JavaScript

python - 计算合并字符串的分数

java - 在使用 itext pdf 生成 pdf 时,CMYK 图像生成的 pdf 尺寸非常大

java - 通过 Spring Social 获取完整 Linkedin 个人资料时出现 400 错误请求错误

android - 检查共享首选项是否为空

python - jinja2 python 的字符串 > Json