送分,X,A,B:single,X=A/B,怎样使 X的类型转换为integer? ( 积分: 50 )

  • 主题发起人 主题发起人 dingcs
  • 开始时间 开始时间
D

dingcs

Unregistered / Unconfirmed
GUEST, unregistred user!
X,A,B的类型为:single,
X=A/B,
怎样使 X的类型转换为integer?
 
var
i, j: Integer;
x,a,b: single;
begin
a := 1000;
b := 22.2;
X := a/b;
I := Round(X);
J := Trunc(X);
end;
 
给你总结总结:
Round(X): 这个函数比较特殊,不是四舍五入的,
Round returns an Int64 value that is the value of X rounded to the nearest whole number. If X is exactly halfway between two whole numbers, the result is always the even number
比如 round(12.5)=12,round(12.51)=13

Trunc(X):这个函数是截取函数,没有四舍五入的概念,截取小数的整数部分;

小数到整数的四舍五入转换,delphi没有提供,要自己去写:
StrToInt(FloatToStr(RoundTo(X,0))
 
将数字四舍五入保留两位小数
function SSWR(s: real): real;
var
r1, r2: real;
s1, s2: string;
begin
r1 := int(s);
r2 := frac(s);
s1 := copy(floattostr(r1), 1, length(floattostr(r1)));
if length(floattostr(r2)) >= 5 then
begin
if strtoint(copy((floattostr(r2)), 5, 1)) >= 5 then
if strtoint(copy((floattostr(r2)), 4, 1)) = 9 then
if strtoint(copy((floattostr(r2)), 3, 1)) = 9 then
begin
s1 := inttostr(strtoint(s1) + 1);
s2 := ';
end
else
S2 := inttostr(strtoint(copy((floattostr(r2)), 3, 1)) + 1)
else if copy((floattostr(r2)), 3, 1) = '0' then
S2 := '0' + inttostr(strtoint(copy(floattostr(r2), 3, 2)) + 1)
else s2 := inttostr(strtoint(copy(floattostr(r2), 3, 2)) + 1)
else s2 := copy(floattostr(r2), 3, 2);
end
else s2 := copy(floattostr(r2), 3, 2);
result := strtofloat(s1 + '.' + s2);
end;
 
楼主有问四舍五入吗?
 

Similar threads

S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
913
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部