【发布时间】: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