
|
If you were logged in you would be able to see more operations.
|
|
|
OGNL
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
|
|
|
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) );
}
|
|
Description
|
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) );
} |
Show » |
|