groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alberto Vilches <vilc...@gmail.com>
Subject Re: Casting Double to BigDecimal
Date Mon, 11 Jan 2016 09:57:22 GMT
Yes, I know the "1.9" literal in Groovy results in a BigDecimal. That's why
I'm trying to force the double using the "d", but maybe is still a
bigdecimal. So, let's try again:

    Double d = 1.9
    println((BigDecimal)d)

prints "1.899999999999999911182158029987476766109466552734375"

and

    Double d = 1.9
    println d as BigDecimal

prints "1.9"



On Fri, Jan 8, 2016 at 1:34 PM, Naresha K. <naresha.k@gmail.com> wrote:

> Value 1.9 will be of type BigDecimal.
> Why are you using D?
>
>
> On Fri, Jan 8, 2016 at 5:45 PM, Alberto Vilches <vilches@gmail.com> wrote:
>
>> Hi everybody! We have an issue in our application, and we realized these
>> two lines have differents results. We wonder why because we think it should
>> calls to the DefaultGroovyMethods.asType(Number self, Class<T> c). But it
>> seems only the explicit call to "as" finally calls to the asType and the
>> casting do a different thing (just a new BigDecimal(1.9D), but we wonder in
>> which part of Groovy is happening)
>>
>> (BigDecimal)1.9D  //
>> "1.899999999999999911182158029987476766109466552734375" ->
>> 1.9D as BigDecimal  // "1.9"
>>
>> In fact, we tried to put these two lines in the Groovy console and see
>> the AST in all the phases. But in all of them shows the same code:
>>
>>    public java.lang.Object run() {
>>         ((1.9) as java.math.BigDecimal)
>>         return ((1.9) as java.math.BigDecimal)
>>     }
>>
>> Somebody please could give some light? Thank you very much and happy new
>> year :)
>>
>> --
>> Un saludo.
>> Alberto Vilches
>>
>
>


-- 
Un saludo.
Alberto Vilches

Mime
View raw message