本文整理自网络,侵删。
delphi 四舍五入保留一位或两位小数写法,后面参数为保留小数点位数
function RoundFloat(f: double; i: integer): double;var s: string; ef: Extended;begin if f = 0 then begin Result := 0; Exit; end; s := '#.' + StringOfChar('0', i); if s = '#.' then s := '#'; ef := StrToFloat(FloatToStr(f)); //防止浮点运算的误差 result := StrToFloat(FormatFloat(s, ef));end;
//调用procedure TForm1.Button1Click(Sender: TObject);beginEdit2.Text:=FloatToStr(RoundFloat(StrToFloat(Edit1.Text),2));end;
相关阅读 >>
Delphi 请求时间,为当前时间,数值为1970-01-01以来的毫秒数
Delphi: ttreeview 中禁止双击事件展开或关闭节点
Delphi application.title在win7下失效了?
Delphi 使用windows api(wincrypt)计算文件md5哈希,支持大文件
更多相关阅读请进入《Delphi》频道 >>