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);

begin
Edit2.Text:=FloatToStr(RoundFloat(StrToFloat(Edit1.Text),2));
end;








相关文章:

  • 2021-08-21
  • 2022-02-27
  • 2021-11-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-18
  • 2022-12-23
  • 2022-01-22
  • 2021-12-23
  • 2022-12-23
  • 2021-12-18
  • 2021-12-18
相关资源
相似解决方案