Spring’s validation support is widely used to validate objects. For example, it can validate models that are coming from user submitted forms. Validation is happening based on annotations that you can put on the fields in your model. These annotations are not part of Spring, external libraries must be used. The most common is the javax.validation package with the hibernate validator implementation.
The values to be validated could be decimal values. These can be represented by a number of different types. Probably the two most common types that pop into your mind are
double. These could work okay, but because they are not exact numbers, just approximations, depending on the validation implementation, there could be some false results.
The best idea for validation is to use the
BigDecimal type with the
@DecimalMax validation annotations.
@DecimalMin(value = "0.1", inclusive = true) @DecimalMax(value = "9.9", inclusive = true) private BigDecimal measurement;
It might seem like a hassle to deal with values of this type, but actually it’s pretty straightforward. You just have to use method calls instead of numeric operators to add / subtract / etc. values.