Date: 2004-05-20T13:32:25
   Editor: RobertBurrellDonkin <[EMAIL PROTECTED]>
   Wiki: Jakarta Commons Wiki
   Page: ValidatorNumberWithSeperators
   URL: http://wiki.apache.org/jakarta-commons/ValidatorNumberWithSeperators

   Moved content from original wiki

New Page:

Up to ValidatorExtensions
----
= Question =
{{{
 Does anyone have any recommendations for validating
 user-entered numbers with separators? (For example,
 the string "10,000.00".) Validator uses the Double
 constructor in its GenericTypeValidator.formatDouble
 method.
}}}

= Answer =

I wound up creating my own custom FieldChecks class
with a validateDouble method that delegates to a new
formatDouble method. Here's the code for formatDouble:
{{{
    public static Double formatDouble(String s) throws
             NumberFormatException {
        DecimalFormatSymbols syms = new DecimalFormatSymbols();
        StringBuffer buff = new StringBuffer();
        StringCharacterIterator iter = new StringCharacterIterator(s);
        for(char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
            if(c != syms.getGroupingSeparator()) {
                buff.append(c);
            }
        }

        return GenericTypeValidator.formatDouble(buff.toString());
    }
}}}

Let me know if anyone has a better solution.

Matt Howitt
----
Up to ValidatorExtensions


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to