四舍五入的BUG
(转http://www.nssoft.net/showdoc.asp?did=267)
关键词:四舍五入
Delphi的四舍五入函数Round有BUG,无法正常工作。
对于XXX.5的情况,整数部分是奇数,那么会Round Up,偶数会Round Down,例如:
x:= Round(17.5) = x = 18
x:= Round(12.5) = x = 12
请使用下面的函数代替Round:
function DoRound(Value: Extended): Int64;
procedure Set8087CW(NewCW: Word);
asm
MOV Default8087CW,AX
FNCLEX
FLDCW Default8087CW
end;
const
RoundUpCW = $1B32;
var
OldCW : Word;
begin
OldCW := Default8087CW;
try
Set8087CW(RoundUpCW);
Result := Round(Value);
finally
Set8087CW(OldCW);
end;
end;