Issue Details (XML | Word | Printable)

Key: OGNL-122
Type: Bug Bug
Status: Resolved Resolved
Resolution: Fixed
Priority: Major Major
Assignee: Jesse Kuhnert
Reporter: Malcolm Edgar
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
OGNL

OGNL Double to BigDecimal type conversion

Created: 11/Sep/07 10:11 PM   Updated: 17/Sep/07 04:21 PM
Component/s: None
Affects Version/s: None
Fix Version/s: 2.7.2

Environment: Ognl 2.6.7 and looks like issue is present in head


 Description  « Hide
Hi All,

There is a problem with the way the ognl.OgnlOps class converts Double values to BigDecimal values, it uses the BigDecimal constructor which takes a Number class rather than the String class. With Java the correct way to initialize a BigDecimal from a Double is to use the Double.toString() otherwise you will get:

2.20000000000000017763568394002504646778106689453125

Instead of:

2.2

The fix for the method is provided below:

      /**
       * Evaluates the given object as a BigDecimal.
       *
       * @param value an object to interpret as a BigDecimal
       * @return the BigDecimal value implied by the given object
       * @throws NumberFormatException if the given object can't be understood as a BigDecimal
       */
    public static BigDecimal bigDecValue( Object value ) throws NumberFormatException
    {
        if (value == null)
            return BigDecimal.valueOf(0L);
        Class c = value.getClass();
        if ( c == BigDecimal.class )
            return (BigDecimal)value;
        if ( c == BigInteger.class )
            return new BigDecimal( (BigInteger)value );
        if ( c.getSuperclass() == Number.class )
             / /return new BigDecimal( ((Number)value).doubleValue() );
         // Fix BigDecimal conversion error
         return new BigDecimal( value.toString() );
        if ( c == Boolean.class )
            return BigDecimal.valueOf( ((Boolean)value).booleanValue()? 1 : 0 );
        if ( c == Character.class )
            return BigDecimal.valueOf( ((Character)value).charValue() );
        return new BigDecimal( stringValue(value, true) );
    }

 All   Comments   Change History      Sort Order: Ascending order - Click to sort in descending order
Jesse Kuhnert added a comment - 17/Sep/07 04:21 PM
Implemented using the preferred String constructor. Thanks.