数据库中有以个字段
类别     值
a        0.1
b        0.2
a        0.3 ....
其中值那一列是float类型,在使用sum(值)求和的时候,所得的
结果是正确的,但是在分组统计的时候,就会出现问题,合计的
值可能出现了7位以上的小数。
请问这是什么原因。有什么方法可以解决吗,不要使用convert(decimal(),...)
的方式,因为要保留的小数位数不一定完全一样。

解决方案 »

  1.   

    float就是这样的,字段用decimal吧,或者干脆将float类型的结果先行转换为numeric数据类型,再进行round()
    ,round(convert(numeric(6,3),col),2)
      

  2.   

    不适用convert就是用cast吧 
      

  3.   

    float,real都是非精确数值类型,也就是查出的数据有可能存在误差,可用程序处理精度,或转换成精确数值类型numeric,decimal
      

  4.   

    LZ需要精确小数点几位?
    比如两位,则可以先乘以1000,取整数,再group by ,结果显示时,sum()/1000,就行了。
      

  5.   


    if object_id('test')is not null drop table test
    --建表
    create table test
    (
      type varchar(10),
       price float
     )
    --插入数据
    set nocount on
    insert into test select 'a',7.8  union all
                     select 'a',4    union all
                     select 'b',0.01 union all
                     select 'c',10   union all
                     select 'b',100 
    --查询
    select type,price
      from test 
    --测试
    select type, sum(round(convert(numeric(6,3),price),2))as price
      from test
    group by type
    --numeric(6,3)3为小数点后的精度
    --round(..,2)2四舍五入为右边小数点位数
      

  6.   

    float,real不精确,可以转换成精确数值类型numeric,decimal--“要保留的小数位数不一定完全一样”?
    那就取最大的小数位数的那个不就可以了--不用convert?
    那用cast吧-.---不用decimal()?
    那用numeric()吧
      

  7.   

    sum(round(convert(numeric(6,3),price),2))as price