You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by "David J. M. Karlsen" <da...@davidkarlsen.com> on 2005/04/21 20:20:16 UTC

Re: [commons-beanutils] Problem with a BigDecimal Conversion

Alex 'Kazuma' Garbagnati wrote:

> Hi guys,
>
>    I was trying to "play around" with BeanUtils conversions and I find 
> out a behaviour that I cannot really understand.
> Here is a simple piece of code to better explain the issue:
>
> --- code starts here ---
>
> import org.apache.commons.beanutils.locale.converters.*;
>
> public class Test {
>
>         public static void main(String[] args) {
>                 String[][] testValues = new String[2][2];
>                 testValues[0][0] = "1.234,000";
>                 testValues[0][1] = "#,##0.000";
>                 testValues[1][0] = "1.234,001";
>                 testValues[1][1] = "#,##0.000";
>
>                 BigDecimalLocaleConverter converter = new 
> BigDecimalLocaleConverter();
>                 for (int i=0; i<testValues.length; i++) {
>                         Object obj = 
> converter.convert(testValues[i][0], testValues[i][1]);
>                         System.out.print("Value = " + obj.toString());
>                         System.out.print(" [" + 
> obj.getClass().getName() + "]");
>                         System.out.print(" (" + testValues[i][0]);
>                         System.out.println(" - " + testValues[i][1] + 
> ")");
>                 }
>         }
> }
> --- code ends here ---
>
> By running this piece of code I obtain this:
>
> Value = 1234 [java.lang.Long] (1234,00000000000 - #,##0.00000000000)
> Value = 1234.00000000001 [java.lang.Double] (1234,00000000001 - 
> #,##0.00000000000)
>
> What I do consider strange is that the returning object is not a 
> BigDecimal, but a Long or a Double.
>
> Is this the correct behaviour? If not, should I (or someone here) 
> report this as a Bug?
>
> And, most important, what is the correct way to convert a string that 
> follows the #,##0.00000000000 to a BigDecimal (always a big decimal, 
> even if the original number has no decimals)?

Nobody responded to this as far as I can see...

It even fails at this:

BigDecimal test = new BigDecimal("1000.21");
BigDecimalLocaleConverter conv = new BigDecimalLocaleConverter();
BigDecimal test2 = (BigDecimal) conv.convert(test.toString());


with a java.lang.Long classcastex.

which really freaks me out as well....what's up?


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org