Java – Safe String to BigDecimal conversion


I'm trying to read some BigDecimal values from the string. Let's say I have this String: "1,000,000,000.999999999999999" and I want to get a BigDecimal out of it. What is the way to do it?

First of all, I don't like the solutions using string replaces (replacing commas etc.). I think there should be some neat formatter to do that job for me.

I've found a DecimalFormatter class, however as it operates through double – huge amounts of precision are lost.

So, how can I do it?

Best Solution

Check out setParseBigDecimal in DecimalFormat. With this setter, parse will return a BigDecimal for you.