一个小问题(50分)

S

seafox

Unregistered / Unconfirmed
GUEST, unregistred user!
怎么截取小数点后2位数把多余的去掉
例如:124.2331 截取为124.23
有四舍五入的函数 例如:124.368 截取为124.37
 
floattostrf()
 
formatefloat('0.00',fnum)
 
现写了一个函数,你试试把,有错误改改
function getNum(Mynum1):Float;
var str1,Str2:String;
ResFloat:Float;
begin
str1:=floattostr(myNum1);
Coun:=pos(str1,'.');
if coun>0 then
begin
mynum2:=strtoint(copy(str1,coun+1,1));
if mynum2>4 then
begin
str2:=copy(str1,1,coun+2);
resFloat:=StrToFloat(Str2)+0.01;

end
else
begin
str2:=copy(str1,1,coun+2);
resFloat:=StrToFloat(Str2);

end;
end
else
exit;
result:=resFloat;
end;
 
灵活一点,*100,四舍五入后,再/100
 
用formatefloat('0.00',124.2331)
得到 123.23
用formatefloat('0.00',124.368)
得到 123.37
不过
用formatefloat('0.00',124.3648)
得到 123.36
不会进位
 
var
s:string;
begin
str(124.368 :0:2,s);

//此时s=124.37
end;

用法说明:
procedure Str(X [: Width [: Decimals ]];
var S);
 
其实用formatfloat函数最简单适用了
 
谢谢各位
 
顶部