Take the 2-minute tour ×

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.

share|improve this question

marked as duplicate by Gilbert Le Blanc, Brian, True Soft, Marko Topolnik, Lev Levitsky Jan 15 '13 at 21:14


Maybe but explanation under is better. –  Yoda Jan 15 '13 at 20:55

2 Answers 2

up vote 7 down vote accepted

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.

share|improve this answer

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)

share|improve this answer
