简体   繁体   中英

How can the scale of a BigDecimal be validated?

We currently have some code for validating a string as a BigDecimal of the correct scale that looks like:

try {
    new BigDecimal(amount).setScale(4);
} catch (Exception e) {
    //...something
}

I would prefer it looked a little more like this:

if(!BigDecimalValidator.getInstance().isValid(amount)){
   ...something 
}

However this validates that the input can be parsed, but does not validate that the scale is correct.

How can this be done using a validator? Is the only way to do this by comparing the scale or catching the exception?

创建一个验证器,并将第一个示例中的代码放入其中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM