Sql server中有一表(CangKu),該表有一字段(Amount)為real類型,用來記錄金額,在後台用select查詢發現有一筆記錄的Amount值為26.725,但用round(Amount,2)後發現結果卻是26.72!而在前台程序中取出該值用round(Amount,2)後的結果是26.73!這樣就導致前後台算的結果有點差異。我想可能是由於real類型存的是近似值的原因,因公司原因Amount不能改為其它類型,請問各位有沒有什麼辦法能使後台round(Amount,2)的結果為26.73?

解决方案 »

  1.   

    declare @dec real
    set @dec=26.725
    select cast(@dec as dec(18,2))
    /*
    ---------------------------------------
    26.73(1 個資料列受到影響)*/
      

  2.   

    decimal(Amount,2) 自动四舍五入的帮你 
      

  3.   

    --結果一樣
    declare @dec real
    set @dec=26.725
    select cast(@dec as dec(18,2))
    select round(@dec,2)
    /*
    ---------------------------------------
    26.73(1 個資料列受到影響)*/
      

  4.   

    不过楼主说的那个问题我这里没有出现
    select round(26.725,2)
    /**
            
    ------- 
    26.730(所影响的行数为 1 行)
    **/
      

  5.   

    select round(26.725,2)
    /*---------------------------------------
    26.730(1 行受影响)*/
      

  6.   

    round会进行四舍五入的。还有在SQL中尽量不要使用real数据类型。。可以使用numeric。。看例子。create table a(num numeric(5,2))
    insert into a select 1
    insert into a select -0.1select sum(num) from a--结果:
    0.90
    create table b(num real)
    insert into b select 1
    insert into b select -0.1select sum(num) from b
    --结果:
    0.899999998509884