I'm working with DecimalFormat
, I want to be able to read and write decimals with as much precision as given (I'm converting to BigDecimal
).
Essentially, I want a DecimalFormat
which enforces the following pattern "\d+(\.\d+)?" i.e. "at least one digit then, optionally, a decimal separator followed by at least one digit".
I'm struggling to be able to implement this using DecimalFormat
, I've tried several patterns but they seem to enforced fixed number of digits.
I'm open to alternative ways of achieving this too.
Edit:
For a little more background, I'm parsing user-supplied data in which decimals could be formatted in any way, and possibly not in the locale format. I'm hoping to let them supply a decimal format string which I can use the parse the data.
Since you noted in a comment that you need Locale support:
And then parse.
This seems to work fine:
Except for the last two that violate your "at least one digit" requirement. You may have to check that separately using a regex if it's really important.