Best way to convert Locale specific String to BigDecimal

别说谁变了你拦得住时间么 提交于 2019-11-27 01:14:41

问题


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

The following code shows my problem:

    String numberString = "2.105,88";

    NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
    try {
        Number parsed = nf.parse(numberString);
        BigDecimal bd1 = new BigDecimal(parsed.toString());
        System.out.println(bd1.toString());

        BigDecimal bd2 = new BigDecimal(parsed.doubleValue());
        System.out.println(bd2);

        BigDecimal bd3 = new BigDecimal(numberString);
        System.out.println(bd3);

    } catch (ParseException e) {
        e.printStackTrace();
    }

The outpout of this is

2105.88

2105.8800000000001091393642127513885498046875

Exception in thread "main" java.lang.NumberFormatException at java.math.BigDecimal.(Unknown Source) at java.math.BigDecimal.(Unknown Source) at test.BigDecimalTest.main(BigDecimalTest.java:22)

The first output is correct, but it doesn't really make sense to convert a String to a Number (Double to be precise), then back to a String again and then into a specific type of Number, BigDecimal.

The second output is incorrect, but could be solved by setting the scale of the BigDecimal. However, the amount of digits is not always known.

The third output is obviously not what I'm looking for.

My question, what would be the best way? Are there better ways to do this?


回答1:


It seems like there is no other way since java.Lang.Number doesn't have a method which returns a BigDecimal type. Anyway it makes sense because BigDecimal only accepts strings which are properly formatted not like ""2.105,88" but like "2105.88" Let me show your my code:

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
public class JavaMain {
    public static void main(String[] args) {
        String numberString = "2.105,88";
        //using casting
        try {
            DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.GERMAN);
            df.setParseBigDecimal(true);
            BigDecimal bd = (BigDecimal) df.parseObject(numberString);
            System.out.println(bd.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //your way short version
        NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
        try {
            BigDecimal bd1 = new BigDecimal(nf.parse(numberString).toString());
            System.out.println(bd1.toString());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String numberStringFixed = "2105.88";
        //direct string formated
        System.out.println(new BigDecimal(numberStringFixed));;     
        //direct but erroneous way if the string is not formated
        System.out.println(new BigDecimal(numberString));;

    }
}

I hope this helps!




回答2:


DecimalFormat has a method called setParseBigDecimal that causes parse() to return a BigDecimal. You just need to cast the returned Number.




回答3:


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);


来源:https://stackoverflow.com/questions/12836647/best-way-to-convert-locale-specific-string-to-bigdecimal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!