如何将货币格式的字符串转换回BigDecimal?(使用Java NumberFormat)

如何将货币格式的字符串转换回BigDecimal?(使用Java NumberFormat),java,android,locale,number-formatting,Java,Android,Locale,Number Formatting,在我的Android应用程序中,我有一个EditText,我从中获取一个数字,并将其转换为BigDecimal,然后再转换为本地货币格式: String s = "100000"; Locale dutch = new Locale("nl", "NL"); NumberFormat numberFormatDutch = NumberFormat.getCurrencyInstance(dutch); Log.e(this, "Currency Format: "+ numberFormatD

在我的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())));
这打印出了10万欧元,与预期一致。然而,我现在想把它转换回一个大十进制


有没有办法将本地格式的货币字符串转换回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);

希望有帮助

当数字的基本表示形式是
s
时,为什么要这样做?可以使用正则表达式从字符串中去掉所有非数字字符,例如str=str.replaceAll(“[^\\d]”,“”),它不能直接回答您的问题,因此,这只是一个你可以采取的方法的建议。@NigelK-不幸的是,这没有考虑到在欧洲大陆(包括荷兰),我们使用逗号作为小数点,而大多数英语国家使用点作为小数点。由于这个原因,这已经失败了。您是否尝试使用numberFormatDutch.parse(myString,newparseposition(0));“有没有办法将本地格式的货币字符串转换回BigDecimal?”-您的回答不会给他一个BigDecimal实例。该修复方法对小数量无效。将
s
设置为“3.14”将
bd
设置为3.14000000000001243449787580175325274467466826171875。是的,您是对的,因为在Java上实现了双重功能