1

Possible Duplicate:
Safe String to BigDecimal conversion

Is there any ultimate parse function in java which can analyze type of the number given in string and return its value in object of class Number or similar. I would like to convert string into bigDecimal or Number and put t into collection, sometimes I have string like 2.23 or 2,23 or 2 or BigInteger like 233242424242424242.

Community
  • 1
  • 1
Yoda
  • 17,363
  • 67
  • 204
  • 344

2 Answers2

12

If all you want is to parse a String to a BigDecimal, you can use the constructor that gets a string as a argument. The constructor takes care of the basic parsing.

BigDecimal b = new BigDecimal(str);

For complex parsing (involving thousands separators), you can use the methods in the DecimalFormat class with setParseBigDecimal set to true.

Nivas
  • 18,126
  • 4
  • 62
  • 76
-2

The documentation of the Double method has an excellent example of the regexp you are looking for: http://docs.oracle.com/javase/6/docs/api/java/lang/Double.html#valueOf(java.lang.String)

Petr
  • 5,999
  • 2
  • 19
  • 25