0
votes

In the Bean Validation spec ( both version JSR 303 and JSR 349 ) and docs, both don't mention anything about @Digits support for Double or Float types. It only says it supports the following types:

  • BigDecimal
  • BigInteger
  • CharSequence
  • byte, short, int, long, and their respective wrapper types

However, I tested it many times against Double fields in my entities and I found that it works well!

Does it have any drawbacks that I can not see? Is it discouraged to use it with Double?

Also I found the documentation does explicitly say that @Min and @Max don't support Double and Float. However, It is not that clear about the @Digits

2
'I tested it many times against Double fields in my entities and I found that it does work well!' You mean It does NOT work ( exception ) or Work but not properly? - Loc
@Loc It works perfectly - fujy

2 Answers

1
votes

Double is a floating point number and can't describe decimal numbers very well. When rounding errors occur and the number of fraction digits can change.

1
votes

Hibernate Validator actually supports @Digits on any Number type and thus also for Double/double. This is done by creating a BigDecimal from the string representation of the given number. As this behavior is not mandated by the spec, it might not be working with other Bean Validation providers.