Announcement Announcement Module
No announcement yet.
Can you specify the maximum number of decimal places for a field? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can you specify the maximum number of decimal places for a field?

    Is it possible to specify the maximum number of decimal places allowed in a given field, using either:
    • a field-level option in the roo shell, or
    • a JSR-303 annotation on that field?
    I have a java.math.BigDecimal field that contains a currency amount, and I want to validate that the amount doesn't contain more than say 3 decimal places (the maximum number of decimal places in any currency, AFAIK). Also I want the maximum number of decimal places to determine the scale of the relevant database column (e.g. NUMBER(10,3)).

    I've found the @DecimalMax annotation, but from reading the JavaDoc, it seems to limit the value of the entire field.

    If there's no way of doing this, is it possible to write your own annotations and have them applied by the JSR-303 provider?
    Last edited by Andrew Swan; Jan 9th, 2010, 07:32 PM. Reason: added requirement about scale of database column

  • #2
    You can define your own annotations which would work in conjunction with JSR-303 Bean Validation. If you take a look at the specification you can find a few examples which describe the process.

    Alternatively you could use the @javax.validation.constraints.Pattern annotation which allows you to specify the exact pattern for your decimal number via a regular expression.



    • #3
      I've recently used @Column, that is a javax.persistence annotation, and it seems its working fine.

      You can see it at:

      @Column(precision=12, scale=2)


      • #4
        For those not bound to annotations you could use Forms4j (, which has an in-built validator for currency.

        <forms4j:input id="foo">
        	<forms4j:validator pattern="MONEY" />