Delphi怎样取小数点后几位 Delphi怎样取小数点后几位,最好能使它四舍五入比如 0.2668 取两位小数是0.27 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 uses mathRoundTo(0.2668,-2)=0.27 四舍五入需要自己写函数这是一个四舍五入的函数,具体用法 myround(1.999,2) = 2.00 第一位1.999为要四舍五入的数,2为要取的小数位。function myround(const yuan: Extended; const pp: Integer): Extended;//yuan:原浮点数,PP保留 小数点后第几位varp,l,m,l2:Longint;s:string; // 原浮点数sq:string; // 小数点前sh:string;//小数点后beginif yuan=0 then exit;// 原浮点数 0if pp<0 then exit; //非法小数点后第几位s:=floattostr(yuan);p:=pos('.',s); //小数点位置sq:=midstr(s,1,p-1);sh:=midstr(s,p+1,length(s)-length(sq)-1);l:=length(sh);//小数位数l2:=length(sq);//整数位数if pp>=l thenbegin//0result:=strtofloat(s);exit;//比如 11。06 要保留到 小数点后第3位显然 不合理end;//{ if pp=l then //比如 11。06 要保留到 小数点后第2位不用处理 直接返回begin//1Result:=s;exit;end;//1 }if pp<l then //比如 11。06 要保留到 小数点后第1位 ,begin//2m:=strtoint(sh[pp+1]);if m>=5 thenbeginif pp>=1 then //保留到 小数点后第1,2位begin//3sh:=midstr(sh,1,pp);sh := inttostr(strtoint(sh)+1);if length(sh)>pp thenbeginsh:= midstr(sh,2,pp);sq:= inttostr(strtoint(sq)+1);end;Result:=strtofloat(sq+'.'+sh);exit;end//3else //保留到 小数点后第0位begin//4sq[l2]:=chr(ord(sq[l2])+1);Result:=strtofloat(sq);exit;end;//4endelsebeginif pp>=1 then //保留到 小数点后第1,2位begin//3sh:=midstr(sh,1,pp);Result:=strtofloat(sq+'.'+sh);exit;end//3else //保留到 小数点后第0位begin//4Result:=strtofloat(sq);exit;end;//4end;end;//2end; 窗体之间传递值,但不破坏对象的封装呢? tcxtreelist单元格属性问题 关于DLL共享应用程序中ADO数据库连接的问题? delphi中是否有类似cb中的substring函数? 为什么一使用msxml的load方法就出错 WORD上图片处理的问题,在线等,谢了! 请教SQL的问题! DBGirdEh中怎么实现列合计?为什么我设的都为零。 请问各位:TStringField,TFloatField等类,在interface里,总提示我没有相应的组件,为什么,他们到底该怎样用才好 如何修改内存? adoconnection 问题 关于TREEVIEW选中某个节点离开焦点时的问题?
RoundTo(0.2668,-2)=0.27
function myround(const yuan: Extended; const pp: Integer): Extended;
//yuan:原浮点数,PP保留 小数点后第几位
var
p,l,m,l2:Longint;
s:string; // 原浮点数
sq:string; // 小数点前
sh:string;//小数点后
begin
if yuan=0 then exit;// 原浮点数 0
if pp<0 then exit; //非法小数点后第几位
s:=floattostr(yuan);
p:=pos('.',s); //小数点位置
sq:=midstr(s,1,p-1);
sh:=midstr(s,p+1,length(s)-length(sq)-1);
l:=length(sh);//小数位数
l2:=length(sq);//整数位数
if pp>=l then
begin//0
result:=strtofloat(s);
exit;//比如 11。06 要保留到 小数点后第3位显然 不合理
end;//
{ if pp=l then //比如 11。06 要保留到 小数点后第2位不用处理 直接返回
begin//1
Result:=s;
exit;
end;//1 }
if pp<l then //比如 11。06 要保留到 小数点后第1位 ,
begin//2
m:=strtoint(sh[pp+1]);
if m>=5 then
begin
if pp>=1 then //保留到 小数点后第1,2位
begin//3
sh:=midstr(sh,1,pp);
sh := inttostr(strtoint(sh)+1);
if length(sh)>pp then
begin
sh:= midstr(sh,2,pp);
sq:= inttostr(strtoint(sq)+1);
end;
Result:=strtofloat(sq+'.'+sh);
exit;
end//3
else //保留到 小数点后第0位
begin//4
sq[l2]:=chr(ord(sq[l2])+1);
Result:=strtofloat(sq);
exit;
end;//4
end
else
begin
if pp>=1 then //保留到 小数点后第1,2位
begin//3
sh:=midstr(sh,1,pp);
Result:=strtofloat(sq+'.'+sh);
exit;
end//3
else //保留到 小数点后第0位
begin//4
Result:=strtofloat(sq);
exit;
end;//4
end;
end;//2
end;