一个实型数据的转换 一个实型数据如何以整型数据存储,有举例来看看。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1.Round(四舍六入五留双)功能说明:对一个实数进行四舍五入。(按照银行家算法) 例:var i, j: Integer;begin i := Round(1.5); // i等于2 j := Round(2.5); // j等于2end;在Delphi中使用Round函数得到的答案有时与我们所预期的会不太一样:采用的是四舍六入五留双。即当舍或入位大于或小于五时按四舍五入来处理,而当舍或入位等于五时,就要看前面一位是什么,根据奇进偶不进,它总是返回一个偶数值。例: i:= Round(11.5)//i等于12i:= Round(10.5)//i等于10这种Round其实是按照银行家算法,统计学上一般都用这种算法,比传统的"四舍五入"要科学。如果要使用传统的"四舍五入"方法,可以使用下面函数:function RoundClassic(R: Real)2.trunc(取得X的整数部分)如:trunc(-123.55)=-123, floor(123.55)=1233.ceil(取得大于等于X的最小的整数)如:ceil(-123.55)=-123, ceil(123.15)=1244.floor(取得小于等于X的最大的整数)如:floor(-123.55)=-124,floor(123.55)=123注:floor和ceil是math unit里的函数,使用前要先Uses Mathfunction Int(X: Extended): Extended;//取整 注意它返回的是Extended类型它也是浮点型哦function Round(X: Extended): Int64;//四舍五入 function Trunc(X: Extended): Int64;//将小数无条件舍去floor 直接往小的取,比如 floor(-123.55)=-124,floor(123.55)=123trunc 直接切下整数,比如 trunc(-123.55)=-123, floor(123.55)=123ceil 直接往大的取,比如 ceil(-123.55)=-123, ceil(123.55)=124round 计算四舍五入,比如 round(-123.55)=-124,round(123.55)=124floor 和 ceil 是 math unit 里的函数,使用前要先 Uses Math。trunc 和 round 是 system unit 里的函数,缺省就可以用。//真正的四舍五入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 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;//2end; to babydog01 你的方法是如何取整。 我要的是把一个实型数据的整数部分和小数部分都用整型数据存储。怎么做??谢谢了 用2个int字段,把一个实数拆开来分开存储? 是这意思吗?那怎么行,比如10.0025,小数部分保存进去后是25,那当然不对了说说为什么要这么做? 可以把single当成cardinal,double当成uint64保存。 差不多吧,不过Delphi有absolute:var d: double; i: uint64 absoulte d;begin d := 1000.123; // 保存i就是保存d了end; FileStream分割问题 XP中,如何屏蔽ctrl+alt+delete? ADOCommand 的问题 ClientDataSet使用问题--只将改变了数据的Data提取出来 BORLAND SOCKET SERVER 连接数问题,请大家指教! 怎么设置webbrowser到编辑模式? DBGrid的编辑问题 咦 ?????? 这是谁的分掉在地上了????????快捡呀。。。。。 菜鸟的简单问题,快来抢分 不小心手工删除了kylix的目录就在也装不上了,咋办? 小弟今年刚毕业,想就以后的发展方向,请教各位大神~~~~~ 代码正确,为何错误,显示一个无法解析外部命令工程(我用的是空文件)
例:
var
i, j: Integer;
begin
i := Round(1.5); // i等于2
j := Round(2.5); // j等于2
end;在Delphi中使用Round函数得到的答案有时与我们所预期的会不太一样:采用的是四舍六入五留双。即当舍或入位大于或小于五时按四舍五入来处理,而当舍或入位等于五时,就要看前面一位是什么,根据奇进偶不进,它总是返回一个偶数值。
例:
i:= Round(11.5)//i等于12
i:= Round(10.5)//i等于10这种Round其实是按照银行家算法,统计学上一般都用这种算法,比传统的"四舍五入"要科学。
如果要使用传统的"四舍五入"方法,可以使用下面函数:
function RoundClassic(R: Real)2.trunc(取得X的整数部分)
如:trunc(-123.55)=-123, floor(123.55)=1233.ceil(取得大于等于X的最小的整数)如:ceil(-123.55)=-123, ceil(123.15)=1244.floor(取得小于等于X的最大的整数)如:floor(-123.55)=-124,floor(123.55)=123注:floor和ceil是math unit里的函数,使用前要先Uses Math
function Int(X: Extended): Extended;//取整 注意它返回的是Extended类型它也是浮点型哦
function Round(X: Extended): Int64;//四舍五入
function Trunc(X: Extended): Int64;//将小数无条件舍去floor 直接往小的取,比如 floor(-123.55)=-124,floor(123.55)=123
trunc 直接切下整数,比如 trunc(-123.55)=-123, floor(123.55)=123
ceil 直接往大的取,比如 ceil(-123.55)=-123, ceil(123.55)=124
round 计算四舍五入,比如 round(-123.55)=-124,round(123.55)=124floor 和 ceil 是 math unit 里的函数,使用前要先 Uses Math。
trunc 和 round 是 system unit 里的函数,缺省就可以用。//真正的四舍五入
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;
你的方法是如何取整。
我要的是把一个实型数据的整数部分和小数部分都用整型数据存储。怎么做??谢谢了
差不多吧,不过Delphi有absolute:
var
d: double;
i: uint64 absoulte d;
begin
d := 1000.123;
// 保存i就是保存d了
end;