Tom Cooper
3/30/2007 3:48:00 PM
Float datatype is incapable of representating some decimal values exactly.
So if you want an exact decimal value, use decimal (or numeric) datatype.
To see what I mean, run
Declare @f float
Set @f = 25011.799999999999
Select ROUND(@f,2) As FloatType , Round(Cast(@f As decimal(19, 8)), 2) As
DecimalType
Tom
"ErikYkema" <erik.ykema@gmail.com> wrote in message
news:1175266634.706928.31390@o5g2000hsb.googlegroups.com...
> Hi,
> I am looking at a float field in a SqlServer2000 table.
> In the database I got the following value: 25011.799999999999.
> I like to export this to flatfile using DTS as 25011.80 or 25011.8
> (rounding on two decimals).
>
> When rounding this using round(x, 2, 1), I get: 25011.790000000001 !
>
> _Why is the round(x, 2, 1) behaving like this anyhow?_
>
> Some samples:
> select pcpc.Credit as c1 -- 25011.799999999999
> , round(pcpc.Credit, 2, 1) as c2 -- 25011.790000000001
> , round(pcpc.Credit, 2) as c3 -- 25011.799999999999
> , cast(cast(pcpc.Credit * 100 as int) as float)/100 as c4 --
> 25011.799999999999
> , convert(nchar(32), round(pcpc.Credit, 2), 0) as c5 --
> 25011.8
> , cast(convert(nchar(32), round(pcpc.Credit, 2), 0) as float) as c6
> -- 25011.799999999999
> from FOO ppc
>
> So a float just can't hold the value 25011.799999999999 exactly, and
> the nearest binary representation is 25011.799999999999 (c6) or
> 25011.790000000001 (c2)? Is that it?
>
> I think I'll stick with column c5 for my download per flatfile to my
> datawarehouse.
>
> Fot those interested: an oracle equivalent:
> select pcpc.Credit as c1 -- 25011.8
> , round(pcpc.Credit, 2) as c3 -- 25011.8
> , to_char(pcpc.Credit, 'FM9999999999.99') -- 25011.8
> from (select 25011.799999999999 as credit from dual) pcpc
>
> Regards,
> Erik Ykema
>