- 註冊時間
- 2006-3-13
- 最後登錄
- 2025-1-10
- 在線時間
- 673 小時
- 閱讀權限
- 200
- 積分
- 417
- 帖子
- 1107
- 精華
- 0
- UID
- 2
  
|
參考樓上的修改成這樣- function RoundF(X: Extended; Decimal: integer = 2): Extended;
- var
- PowerNum: Extended;
- begin
- PowerNum := IntPower(10, Decimal);
- if X < 0 then
- Result := Round((X * PowerNum) - 0.1) / PowerNum
- else
- Result := Round((X * PowerNum) + 0.1) / PowerNum;
- end;
複製代碼 測試程式- unit Unit1;
- interface
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, Math;
- type
- TForm1 = class(TForm)
- Memo1: TMemo;
- procedure FormCreate(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.DFM}
- function RoundF(X: Extended; Decimal: integer = 2): Extended;
- var
- PowerNum: Extended;
- begin
- PowerNum := IntPower(10, Decimal);
- if X < 0 then
- Result := Round((X * PowerNum) - 0.1) / PowerNum
- else
- Result := Round((X * PowerNum) + 0.1) / PowerNum;
- end;
- procedure TForm1.FormCreate(Sender: TObject);
- var
- f: Double;
- begin
- Memo1.Clear;
- f := 1213.245;
- Memo1.Lines.Append(floattostr(f));
- Memo1.Lines.Append(FloatToStr(RoundF(1213.245, 2)));
- Memo1.Lines.Append(FloatToStr(RoundF(f, 2)));
- Memo1.Lines.Append('');
- f := -1213.245;
- Memo1.Lines.Append(floattostr(f));
- Memo1.Lines.Append(FloatToStr(RoundF(-1213.245, 2)));
- Memo1.Lines.Append(FloatToStr(RoundF(f, 2)));
- end;
- end.
複製代碼 執行結果
|
|