开发者

Big Decimal parsing problem

开发者 https://www.devze.com 2023-01-27 14:08 出处:网络
I am facing problem in big decimal 开发者_StackOverflow社区no. Following code snippet will explain my problem:

I am facing problem in big decimal 开发者_StackOverflow社区no. Following code snippet will explain my problem:

 BigDecimal parsedValue = (BigDecimal) decimalFormat.parse(input);

Here input is a string type. Now suppose value of input is 135abc24 in this case value of parsedValue is 135 but i want to check for such inputs and give an error instead of truncating the string and rest of part. Just want to add input string may also contains exponential nos, so i cant even check for only numeric strings. Please let me know if you want further information or or question is not clear,

Thanks in advance.


You can use BigDecimal's String constructor to create the BigDecimal (see the link for details) and apply any transformations you wish to your input string beforehands. The constructor throws a NumberFormatException if the input is not a valid representation.


According to the documentation, DecimalFormat.parse() supports a second argument in the form of a ParsePosition reference, which is updated with the position at which the parsing stopped. You can then compare that to the beginning of your string, and determine if the entire string was accepted or not.

0

精彩评论

暂无评论...
验证码 换一张
取 消