cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thomas Lutz <>
Subject [CForms] Bug when converting Doubles and BigDecimals...
Date Thu, 15 Sep 2005 16:08:51 GMT
Hi list !

Starting with from 
the users list I found a bug (at least I think it is one) in the

Setup (code snippets below):
-a form with a field widget that has a datatype decimal (and some 
formatting to make it look pretty)
-a binding that converts from the xml to a decimal
-display the form enter something like: 
-this number is rejected by database validation, because it's to large
-redisplay the form, and, the orginal submitted value has been rounded 
by the convertor to something like 1.2345E60 because

-> in the line
is missing.

So decimalFormat.parse(value) returns a double, and no BigDecimal, which 
results in "loosing accuracy".

Another thing that could be optimized is that decimalFormat.parse(value) 
is used, and not decimalFormat.parse(value,ParsePosition). If the 
ParsePosition would be compared with the length of value, you could be 
sure that e.g. trailing alphachars would cause an error.

So IMHO something like:

value = value.replace(' ', (char)160);
DecimalFormat decimalFormat = getDecimalFormat(locale, formatCache);
decimalFormat.setParseBigDecimal(true); // NEW
Number decimalValue;

ParsePosition parsePosition = new ParsePosition( 0 );
decimalValue = decimalFormat.parse(value, parsePosition);
// NEW ask for error index and compare lenght instead of catching the 
parsing exception
if ( ( parsePosition.getErrorIndex() != -1 ) || ( 
parsePosition.getIndex() != value.length() ) ) {
  return ConversionResult.create("decimal");

would be better.

Shall I supply a patch ? Which other classes should I have a look at ?


Code snippets:
Form definition:
<fd:field id="praemieNtoVtg" required="true">
    <fd:datatype base="decimal">
    <fd:convertor variant="number">

Form binding:
<fb:value id="praemieNtoVtg" path="field[@name='praemieNtoVtg']">
<fd:convertor datatype="decimal"/>

View raw message