![](/img/trans.png)
[英]How to convert number value representing eg percents, currency to string and back to BigDecimal by using DecimalFormat
[英]How to convert currency formatted String back to a BigDecimal? (using Java NumberFormat)
在我的Android應用程序中,我有一個EditText,我從中獲取一個數字,然后將其轉換為BigDecimal,並從那里轉換為本地貨幣格式:
String s = "100000";
Locale dutch = new Locale("nl", "NL");
NumberFormat numberFormatDutch = NumberFormat.getCurrencyInstance(dutch);
Log.e(this, "Currency Format: "+ numberFormatDutch.format(new BigDecimal(s.toString())));
這打印出€100.000,00與預期一樣。 然而,我現在想要將其轉換回BigDecimal。
有沒有辦法可以將本地格式的貨幣字符串轉換回BigDecimal?
String s = "100000";
Locale dutch = new Locale("nl", "NL");
NumberFormat numberFormatDutch = NumberFormat.getCurrencyInstance(dutch);
String c = numberFormatDutch.format(new BigDecimal(s.toString()));
System.out.println("Currency Format: "+ c);
try {
Number d = numberFormatDutch.parse(c);
BigDecimal bd = new BigDecimal(d.toString());
System.out.println(bd);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
貨幣形式:€100.000,00
100000
在您的代碼示例中,您沒有將BigDecimal分配給某些東西,因此您無法將其轉換回來。
//Assign BigDecimal
BigDeciaml x = new BigDecimal(s.toString());
//Your code line
Log.e(this, "Currency Format: "+ numberFormatDutch.format(x));
//x it's the same as before
System.out.println(x);
希望能幫助到你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.