Best way to convert Locale specific String to BigDecimal

前端 未结 3 1125
耶瑟儿~
耶瑟儿~ 2020-12-05 02:04

I have to convert a German locale formatted String to a BigDecimal. However, I\'m struggling with the best solution.

The following code sho

3条回答
  •  臣服心动
    2020-12-05 02:31

    The Problem is the formatting of your String. You should go with the 3rd Method, but format your String like this: String numberString = "2105.88";

    You will be able to see this from the given examples here: Link to Java Api

    Edit: Ok, so I see 2 possible solutions for this. Either go with your solution 1 (That's what I prefer) or do something like:

    numberString = numberString.replaceAll(".", "");
    numberString = numberString.replaceAll(",", ".");
    BigDecimal bd3 = new BigDecimal(numberString);
    

提交回复
热议问题