[hibernate-dev] [Bean Validation] @Max / @Min as double instead of long

Elias Ross genman at noderunner.net
Thu Mar 5 04:30:49 EST 2009


On Wed, Mar 4, 2009 at 4:20 PM, Emmanuel Bernard <emmanuel at hibernate.org> wrote:

> I mean we could but we would need to rely on the string representation
>
> @interface Max {
>  String value()
> }
>
> @Max("12.5")
> double someNumber;

There's very few domains where you want to be validating user input
and storing it as a double. For reasons you talked about and also just
for returning back what the user typed in a form, for instance,
doubles aren't user friendly. Thus, I don't see why validating a
double should be necessarily supported. Most developers people don't
understand floating point very well, and your average user is unlikely
to know about rounding errors and inexact representation, etc.

In my original question, I was considering validation cases when you
are using a BigDecimal. The common use case is validating a column
from a database. Databases columns most often use exact
representations of decimal numbers, for obvious reasons when working
with money. There, you don't want to be storing data in doubles! In
any case, I would assume you'd want to indicate something similar to:

@Max("12.5")
BigDecimal someNumber;

Since the BigDecimal and BigInteger have String constructors that are
well defined, a String representation seems fine.

There's other restrictions you may want to make as well. For instance,
you may want to restrict values to at most N decimal places or
significant digits, i.e. precision. This is useful in scientific
applications. How might you represent that?




More information about the hibernate-dev mailing list