# 哪位高手能给个好用的四舍五入函数

www.MyException.Cn  网友分享于：2013-04-24  浏览：18次

------解决方案--------------------
Format或formatfloat不就行了么
------解决方案--------------------
Trunc(i+0.5)
------解决方案--------------------
round(n+0.000005);
------解决方案--------------------
function RoundClassic(R: Real): Int64;
begin
Result:= Trunc(R);
if Frac(R) >= 0.5 then
Result:= Result + 1;
end;
------解决方案--------------------
10.45 -> 10
10.50 -> 11
8.49 -> 8
9.96 -> 10
12.58 -> 13

------解决方案--------------------
uses Math;

function MyRound(Value: Extended; RoundMode: TFPURoundingMode = rmUp): Int64;
var
RM: TFPURoundingMode;
begin
RM := GetRoundMode;
try
SetRoundMode(RoundMode);
Result := Round(Value);
finally
SetRoundMode(RM);
end;
end;

------解决方案--------------------
wzwcn 这个方法可行！

------解决方案--------------------
round()是四舍五入；
trunc()取整数；
ceil()有小数位，就向整数位进一；
format()取成统一格式的字符串.
------解决方案--------------------

------解决方案--------------------
//四舍五入保留小数点后len位数
function Tsys_njqlrb_f.RoundClassic(R: Real;len:integer): real;
var
i,j:integer;
str:string;
begin
if Trunc(R)=R then
begin
Result:= Trunc(R);
exit;
end;
Result:= Trunc(R);
str:=(copy(floattostr(R),length(inttostr(Trunc(R)))+2,len));
str:='0.'+str;
Result:=Result+strtofloat(str);
j:=1;
for i:=1 to len do j:=j*10;
if length(copy(floattostr(R),length(inttostr(Trunc(R)))+2+len,1))<>0 then
begin
if strtoint(copy(floattostr(R),length(inttostr(Trunc(R)))+2+len,1)) >= 5 then
Result:= Result + 1/j;
end;
end;

------解决方案--------------------
Delphi(Pascal) code
```
function DealPrecision(number:real;precision:integer;dealtype:integer):real;
var
tmpNumber:real;
tmpPrecision:integer;
begin
if Abs(number)<0.0001 then
begin
Result:=0;
Exit;
end;
if precision>=0 then
begin
Result:=DealPrecisionZ(number,precision,dealtype);
exit;
end
else
begin
tmpPrecision:=abs(precision);
tmpNumber:=number/power(10,tmpPrecision);
tmpNumber:=DealPrecisionZ(tmpNumber,1,dealtype);
tmpNumber:=tmpNumber*power(10,tmpPrecision);
Result:=tmpNumber;
end;
end;

function DealPrecisionZ(number:real;precision:integer;dealtype:integer):real;
var
s:string;
i:integer;
tmp:string;
strNumber:string;
intLength,intIntCount,intPointPos,intFloatCount:integer;
intZFFlag:Integer;
begin
if Abs(number)<0.0001 then
begin
Result:=0;
Exit;
end;
if number<0 then
begin
intZFFlag:=-1;
end
else
begin
intZFFlag:=1;
end;
number:=intZFFlag*number;
strNumber:=floattostr(number);
if copy(strNumber,1,15)='0.0999999999999' then
begin
strNumber:='0.1';
end;

if pos('.',strNumber)>0 then
begin
if pos('0000000000',strNumber)>pos('.',strNumber) then
begin
strNumber:=copy(strNumber,1,(pos('0000000000',strNumber)-1));
end;
end;

intPointPos:=Pos('.',strNumber);
if intPointPos=0 then
begin
number:=intZFFlag*number;
result:=number;
exit;
end;
intLength:=length(strnumber);
intIntCount:=intPointPos-1;
intFloatCount:=intLength-intPointPos;
s:='0.';
for i:=1 to (precision+1) do
begin
s:=s+'0';
end;
case dealtype of
0 :
begin
number:=StrToFloat(copy(FloatToStr(number),1,intPointPos+(Length(s)-2)));
tmp:=FormatFloat(s,(number+(5/(power(10,precision+1)))));
number:=strtofloat(copy(tmp,0,length(tmp)-1));
end;
1 :
begin
s:='0.';
for i:=1 to (precision-1) do
begin
s:=s+'0';
end;
s:=s+'1';
if precision<intFloatCount then
begin
number:=strtofloat(copy(strnumber,0,intIntCount+1+precision))+strtofloat(s);
end;
end;
2 :
begin
number:=strtofloat(copy(strnumber,0,intIntCount+1+precision));
end;
end;
number:=intZFFlag*number;
result:=number;
end;```