谁能给个好用的四舍五入函数?
自带的round 好像跟我们平常说 四舍不入不太一样 比如round(10.5)=10
上网找了一个 但识别不出来 10.45=5这样的功能。
[解决办法]
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;
例:myRound(10.45)->10.5
[解决办法]
wzwcn 这个方法可行!
[解决办法]
round()是四舍五入;
trunc()取整数;
ceil()有小数位,就向整数位进一;
format()取成统一格式的字符串.
[解决办法]
如果是实数到整数的 +0.5吧~
实数的以此类推
[解决办法]
//四舍五入保留小数点后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;
代码是转摘的,希望能帮到楼主
[解决办法]
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;