Jim White | 9 May 23:07

Re: [groovy-user] Groovy exception when using sql server money datatype and zero value

Steve Kuekes wrote:
> I've got a sqlserver database that has columns that are money 
> datatypes.  When I read them in using groovy with the following program 
> they work ok, until I get to a value that is 0.0 in the database, then I 
> get the following exception:
> 
> Caught: java.lang.IllegalArgumentException: Digits < 0
>         at org.ppa.DigitError$_main_closure1.doCall(DigitError.groovy:28)
>         at org.ppa.DigitError.main(DigitError.groovy:27)

Yeah, that's printf, although I get a different message with the IAE.

A float formatter will take a float or BigDecimal, but not int.

Conversions are divided into the following categories:

General - may be applied to any argument type

Numeric
    Integral - may be applied to Java integral types: byte, Byte, short, 
Short, int and Integer, long, Long, and BigInteger
    Floating Point - may be applied to Java floating-point types: float, 
Float, double, Double, and BigDecimal

So if you use a floating point format you need to be sure to supply an 
argument with a decimal point.

But when the Groovy SQL type conversions are being done, it sees the 
zero value and chooses Integer for the type.

> ...       
>         db.eachRow("select testmoney from javaweb.dbo.money") { money ->
>             printf "Money is %4.2f\n",money.testmoney
>         }

You can do it this way:

db.eachRow("select testmoney from javaweb.dbo.money") { money ->
    printf "Money is %4.2f\n", money.testmoney as Float
}

or better for monetary amounts:

db.eachRow("select testmoney from javaweb.dbo.money") { money ->
    printf "Money is %4.2f\n", money.testmoney as BigDecimal
}

But I suspect the real problem is your DDL, which you didn't show.  I'm 
thinking you used 'int' for the type.  If you used something like 
numeric(10,2) then this problem won't arise (or at least doesn't for me).

Jim

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Gmane