自带的round 好像跟我们平常说 四舍不入不太一样 比如round(10.5)=10
上网找了一个 但识别不出来 10.45=5这样的功能。
上网找了一个 但识别不出来 10.45=5这样的功能。
解决方案 »
- D7中的corbacon
- 100分:用data pump 从Access数据库向其它数据库转换数据,转换之后字段非空属性和索引都到哪里去了?如何解决?
- 这样的C++代码怎么转换为Delphi?
- 公司门口的标牌怎么写?拜托各位!!!
- 怎么把长度为X的字符串变为长度为X/2的字符串
- 系统中,如何双击一个文件名(或者用open),或者其它方法,执行这个文件?比如,一个word文件,点这个文件名,就用word打开
- 如何分辨ServerSocket1ClientRead接收的数据?
- Sql语句中选择两个时间之间的纪录语句怎么写?
- Delphi
- 用原生ADO对象时,变量如何定义? 直接写cn:=createoleobject("adodb.connection"); 总是提示变量cn 未定义?
- 问一个IDHTTP控件的问题
- button上面的字符自动折行问题
begin
Result:= Trunc(R);
if Frac(R) >= 0.5 then
Result:= Result + 1;
end;
10.50 -> 11
8.49 -> 8
9.96 -> 10
12.58 -> 13
试了一下,应该没问题
var
RM: TFPURoundingMode;
begin
RM := GetRoundMode;
try
SetRoundMode(RoundMode);
Result := Round(Value);
finally
SetRoundMode(RM);
end;
end;例:myRound(10.45)->10.5
trunc()取整数;
ceil()有小数位,就向整数位进一;
format()取成统一格式的字符串.
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;
代码是转摘的,希望能帮到楼主
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;
var
RM: TFPURoundingMode;
begin
RM := GetRoundMode;
try
SetRoundMode(RoundMode);
Result := Round(Value);
finally
SetRoundMode(RM);
end;
end;
举例:
i := RoundEx(11.5) 结果: i=12
i := RoundEx(10.5) 结果: i=11
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;
(旺旺贝贝)
也出手了呀