【问题标题】:Android/Java - Convert double price to String, with a currency parameter givenAndroid/Java - 将双倍价格转换为字符串,并给出货币参数
【发布时间】:2014-05-20 09:52:55
【问题描述】:

我知道它已经被问过很多次了,我确实设法在这个问题上获得了很多 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 时,我在DecimalFormatter formatter = new DecimalFormatter("€##.###,##"); 得到一个IllegalArgumentException。有谁知道我的DecimalFormatter prefix 有什么问题吗?

当我从我的Formatter prefix 中删除$ 时,我也会得到一个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 的回答,因为他在编辑后涵盖了我想要的大部分内容。不过,这里是带有“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;
}

【问题讨论】:

  • 你为什么不在strings.xml中创建一个类似“%1$s %2$s”的资源,然后在你的方法中:public static String doubleToPrice(double price, String currency){ return getResource().getString(R.string.price, price+"", currency) }
  • @EmilPana 我曾经这样做过。但是分隔符是这里的问题..
  • 对不起,代替price+"",换成"new DecimalFormat("##,###.##").format(price)"
  • 在 strings.xml 中创建 "$%1$,.2f 并按照 EmilPana 的说明编写方法 public static String doubleToPrice(double price, String currency){ return String.format(getResource().getString(R.string.price), currency); }

标签: java android string formatting currency


【解决方案1】:

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

【讨论】:

  • 您的答案看起来有点不完整。例如,它不会将 7.3 格式化为 7,30;根据给定的货币参数,fr_FR 和 en_US 格式也没有区别。我对我的主要帖子进行了编辑,以更清楚地说明我想要实现的目标。
  • 我已经对我的代码进行了更改。这应该可以满足您的所有需求,但是“//应该返回“9,-”目前我想不出一种方法来实现这种模式,除非我们做一些自定义黑客...
  • 谢谢,在进行了一些更改并包括一些用于将结尾 00 转换为 - 的自定义技巧之后,您的回答成功了。将我的完整解决方案代码添加到主帖中,并接受您的正确答案。
猜你喜欢
  • 1970-01-01
  • 2014-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-30
相关资源
最近更新 更多