1

In my Java code I have four number in text format (String): Es.

String s1 = "1234.56";
String s2 = "1235,56";
String s3 = "1,234.56";
String s4 = "1.234,56";

I want to convert these Strings into BigDecimal:

BigDecimal b1 = new BigDecimal(s1);
...
...

Obviously it does not work, because BigDecimal does not accept all formats (strings with commas). There is a general way that allows me to convert all strings in BigDecimal?

fuerteVentura22
  • 93
  • 1
  • 2
  • 13

3 Answers3

1

Decimal part is allways locale dependent, so you can use the NumberFormat class and use the parsed result as string to get a BigDecimal from it

Example:

final NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
final Number number = format.parse("3,1415");
final BigDecimal bd = new BigDecimal(number.toString());
System.out.println(bd);
Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

What about DecimalFormat ? Doc here

You just have to define a pattern for decimal separator before parsing the string...

 DecimalFormat decimalFormat = new DecimalFormat("#,###.##");
 decimalFormat.setParseBigDecimal(true);
 BigDecimal bigDecimal = (BigDecimal) decimalFormat.parse("1,234.56");
 System.out.println(bigDecimal);

Should do the trick

BenB
  • 146
  • 1
  • 7
0

To cover all cases I would suggest following impl:

private static BigDecimal convertToBigDecimal(String s) {
    StringBuilder sb = new StringBuilder();

    if (s.indexOf('.') != -1 && s.indexOf(',') != -1) {
        s = s.replace(',', '.');
        String[] ss = s.split("\\.");
        for (int i = 0; i < ss.length; i++) {
            if (i > 0 && i == ss.length - 1) {
                sb.append('.');
            }
            sb.append(ss[i]);
        }
    } else if (s.indexOf('.') != -1) {
        String[] ss = s.split("\\.");
        if (ss.length > 2) {
            sb.append(s.replace(".", ""));
        } else {
            // assume it is decimal delimiter
            sb.append(ss[0]).append('.').append(ss[1]);
        }
    } else if (s.indexOf(',') != -1) {
        String[] ss = s.split(",");
        if (ss.length > 2) {
            sb.append(s.replace(",", ""));
        } else {
            // assume it is decimal delimiter
            sb.append(ss[0]).append('.').append(ss[1]);
        }
    }

    BigDecimal bd = new BigDecimal(sb.toString());
    return bd;
}

The ideas is first check if we have both than we definitely have decimal part and we replace all , to . and then skip all . except last one. Otherwise if we have several same delimiters it is number without decimal part. In other case we need to know pattern to distinguish with decimal or not. For sample numbers we assume it is with decimal.

Mike Shauneu
  • 3,201
  • 19
  • 21
  • For "1,234,567" (no decimal part) the result will be wrong... (1234.567) But that's the best shot so far... – BenB Oct 04 '16 at 14:53
  • You are right @BenB. It is unreal to distinguish without know a pattern for case 111,222 whether it is 1112222 or 111.222. – Mike Shauneu Oct 04 '16 at 15:21