3

When I want to parse value from String array to BigDecimal I have this error:

Exception in thread "main" java.text.ParseException: Unparseable number: "86400864008640086400222"

I am searching on internet for the solution how to fix this. Maybe you guys know?

It was not my idea to use BigDecimal but unfortunately I have to. I founded some code which supposed to change value from String to BigDecimal and return it:

public static BigDecimal parseCurrencyPrecise (String value) throws ParseException
{
    NumberFormat  format = NumberFormat.getCurrencyInstance ();
    if (format instanceof DecimalFormat)
        ((DecimalFormat) format).setParseBigDecimal (true);

    Number  result = format.parse (value);
    if (result instanceof BigDecimal)
        return (BigDecimal) result;
    else {
        // Oh well...
        return new BigDecimal (result.doubleValue ());
    }
}

Here is my code when try to parse:

public void Function() throws ParseException {
    String [] array;
    array=OpenFile().split("\\s");
    for(int i = 10 ;i < array.length; i+= 11) {
        BigDecimal EAE = parseCurrencyPrecise(array[i]);
        System.out.println(EAE);
    }
}

OpenFile function open the file with data and read this in this wayL temp+=line+" "; This is the reason why i split by \s. This work for me with Strings and Integers but I have got problem with BigDecimal.

Greetings,

mkobit
  • 43,979
  • 12
  • 156
  • 150
Agnes
  • 33
  • 1
  • 5
  • Is there a currency sign for your locale before the really large number? I can duplicate this error with a currency sign, but it works when I use a dollar sign (currency for my locale). – rgettman Jan 05 '15 at 21:14
  • What is going on with this local sign? I started using BigDecimal today and I heard some article about local signs. – Agnes Jan 05 '15 at 21:16
  • Please use `BigDecimal.valueOf(result.doubleValue ())` instead of `new BigDecimal (result.doubleValue ())`. – Tom Jan 05 '15 at 21:18
  • 3
    Please don't use EITHER `BigDecimal.valueOf(result.doubleValue())` OR `new BigDecimal(result.doubleValue())`, since both of them will cause you to lose precision with some very big numbers. It's best to forget about the `NumberFormat`, and just use the constructor of `BigDecimal` that takes a `String` as its argument. – Dawood ibn Kareem Jan 05 '15 at 21:23

2 Answers2

3

Instead of dealing with the parsing yourself you could make use of the BigDecimal(String val) constructor. From the Javadoc

BigDecimal(String val)
Translates the string representation of a BigDecimal into a BigDecimal

For example:

BigDecimal bigDecimal = new BigDecimal("86400864008640086400222");

See the Javadoc for the formats the constructor takes.

mkobit
  • 43,979
  • 12
  • 156
  • 150
  • You guys Mike Kobit and markbernard are right it works fine for me too. Thank you all for help – Agnes Jan 05 '15 at 21:31
2

Is there any reason you can't use the constructor? It looks like you are making it more complicated than it has to be. This worked for me:

System.out.println(new BigDecimal("86400864008640086400222"));
markbernard
  • 1,412
  • 9
  • 18