Check if bigdecimal only has 2 digits after precision using @ javax.validation.constraints.Digits

I have the following class.

Class Item
{
   private BigDecimal amount;

   ....
}

How can I check the quantity, which should only contain two digits after accuracy. iee

2.19 correct

and

2.292 wrong

using annotation @javax.validation.constraints.Digits And how to show my own error message for this?

Thank:)

+4
source share
2 answers

Mark the annotation of amountthe class field Itemas follows

class Item {

   @Digits(integer=6, fraction=2, message="{javax.validation.constraints.Digits.message}")
   private BigDecimal amount;

}

In the above class, integerboth are fractionrequired, and the message is an optional parameter for @Digit, and there we can configure our own error message.

ValidationMessages.properties . -..

javax.validation.constraints.Digits.message = "custom error message will be here"
+6

@Digits:

@Digits(integer = 6, fraction = 2)
public String getAmount()
{
   return amount;
}
+1

Source: https://habr.com/ru/post/1544139/


All Articles