Quantcast

Double and BigDecimal

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Double and BigDecimal

adwin grails
Hi,

I have calculation that need to be done .. but sometimes the result returned as Double and sometimes it return as BigDecimal. 

for example:

def amount = 100 ;

def calculator = amount / 26 * 12  ; // if I println(calcaltor.class) ; --> it will return BigDecimal

I tried to round the result so that the calculator:
def calculator = (amount / 26 * 12).setScale(2, BigDecimal.ROUND_HALF_UP)  ;
it produce errors and said that the result is Double and have no idea about SetScale.

so I add new BigDecimal(amount / 26 * 12).setScale(2, BigDecimal.ROUND_HALF_UP)  ;


I have no idea why Double and BigDecimal mixed up 
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Double and BigDecimal

pledbrook
> def amount = 100 ;
> def calculator = amount / 26 * 12  ; // if I println(calcaltor.class) ; -->
> it will return BigDecimal
> I tried to round the result so that the calculator:
> def calculator = (amount / 26 * 12).setScale(2, BigDecimal.ROUND_HALF_UP)  ;
> it produce errors and said that the result is Double and have no idea about
> SetScale.
> so I add new BigDecimal(amount / 26 * 12).setScale(2,
> BigDecimal.ROUND_HALF_UP)  ;
>
> I have no idea why Double and BigDecimal mixed up

The above code works fine in the Groovy 1.7 console. Which version of
Grails/Groovy are you using? Perhaps there was a bug in one of the
earlier Groovy versions.

Peter

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Double and BigDecimal

adwin grails
I'm using grails 1.2.2 
may be have bugs on it. ... 

On Mon, May 24, 2010 at 2:00 PM, Peter Ledbrook <[hidden email]> wrote:
> def amount = 100 ;
> def calculator = amount / 26 * 12  ; // if I println(calcaltor.class) ; -->
> it will return BigDecimal
> I tried to round the result so that the calculator:
> def calculator = (amount / 26 * 12).setScale(2, BigDecimal.ROUND_HALF_UP)  ;
> it produce errors and said that the result is Double and have no idea about
> SetScale.
> so I add new BigDecimal(amount / 26 * 12).setScale(2,
> BigDecimal.ROUND_HALF_UP)  ;
>
> I have no idea why Double and BigDecimal mixed up

The above code works fine in the Groovy 1.7 console. Which version of
Grails/Groovy are you using? Perhaps there was a bug in one of the
earlier Groovy versions.

Peter

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

   http://xircles.codehaus.org/manage_email



Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Double and BigDecimal

pledbrook
> I'm using grails 1.2.2
> may be have bugs on it. ...

Can you show the full code that exhibits this problem? I just tried
with "grails console" (version 1.2.2) and it works fine.

Peter

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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Double and BigDecimal

Finn Herpich | Marfinn Software GmbH
In reply to this post by adwin grails
I've similiar problems with Grails 1.2.2 and 1.3.1 running on Ubuntu.
When I write something like this

Double x = 0.0

It sometimes fails with an exception that BigDecimal can't be cast to Double while in other classes (on the same project) the same code runs perfectly.

Since I'm a little bit in rush with the project I haven't tested it, but so far I could not see any pattern when this error shows up.

Cheers

----- Original Message -----
From: Peter Ledbrook
[mailto:[hidden email]]
To: [hidden email]
Sent: Mon, 24
May 2010 10:07:45 +0200
Subject: Re: [grails-user] Double and BigDecimal


> > I'm using grails 1.2.2
> > may be have bugs on it. ...
>
> Can you show the full code that exhibits this problem? I just tried
> with "grails console" (version 1.2.2) and it works fine.
>
> Peter
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>

Marfinn Software GmbH
Firmensitz: Endenicher Straße 262, 53121 Bonn
Registergericht: Amtsgericht Bonn, HRB 16233
Geschäftsführer: Martin Hensel, Finn Herpich


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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Double and BigDecimal

adwin grails
Mine ... I am not declare as Double, I just using def for variable.

just 

 def amount = (total/12*52).setScale(2,BigDecimal.ROUND_UP);

so I need to fix it and becomes
 def amount = new BigDecimal(total/12*52).setScale(2,BigDecimal.ROUND_UP);

it sometimes produce errors.


On Mon, May 24, 2010 at 3:30 PM, Finn Herpich <[hidden email]> wrote:
I've similiar problems with Grails 1.2.2 and 1.3.1 running on Ubuntu.
When I write something like this

Double x = 0.0

It sometimes fails with an exception that BigDecimal can't be cast to Double while in other classes (on the same project) the same code runs perfectly.

Since I'm a little bit in rush with the project I haven't tested it, but so far I could not see any pattern when this error shows up.

Cheers

----- Original Message -----
From: Peter Ledbrook
[mailto:[hidden email]]
To: [hidden email]
Sent: Mon, 24
May 2010 10:07:45 +0200
Subject: Re: [grails-user] Double and BigDecimal


> > I'm using grails 1.2.2
> > may be have bugs on it. ...
>
> Can you show the full code that exhibits this problem? I just tried
> with "grails console" (version 1.2.2) and it works fine.
>
> Peter
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>
>

Marfinn Software GmbH
Firmensitz: Endenicher Straße 262, 53121 Bonn
Registergericht: Amtsgericht Bonn, HRB 16233
Geschäftsführer: Martin Hensel, Finn Herpich


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

   http://xircles.codehaus.org/manage_email



Loading...