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?
set @dec=26.725
select cast(@dec as dec(18,2))
/*
---------------------------------------
26.73(1 個資料列受到影響)*/
declare @dec real
set @dec=26.725
select cast(@dec as dec(18,2))
select round(@dec,2)
/*
---------------------------------------
26.73(1 個資料列受到影響)*/
select round(26.725,2)
/**
-------
26.730(所影响的行数为 1 行)
**/
/*---------------------------------------
26.730(1 行受影响)*/
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