var
a,b:double;
begin
a:=89.123456;
b:=StrToFloat(Format('%10.2f',[a]));
end;或:
var
a,b:double;
begin
a:=89.123456;
b:=StrToFloat(Format('%*.*f',[10,2,a]));
end;
a,b:double;
begin
a:=89.123456;
b:=StrToFloat(Format('%10.2f',[a]));
end;或:
var
a,b:double;
begin
a:=89.123456;
b:=StrToFloat(Format('%*.*f',[10,2,a]));
end;
Format('%8.2f', [123.456]).
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TNumEdit = class(TCustomEdit)
private
{ Private declarations }
FAutoRounding : Boolean;
FRound : Boolean; // Rounding
FRoundingDecimals : Integer; // Number of decimals for rounding
FKeyPressed : Boolean; // Keypressed
FAlignment : TAlignment;
Function RoundedText(aText : String) : String; // Perform the rounding
property RoundingDecimals : Integer read FRoundingDecimals write FRoundingDecimals;
property Round : boolean read FRound write FRound default false;
protected
procedure KeyPress(var Key: Char); override;
procedure DoExit; override;
procedure DoEnter; override;
procedure Change; override;
public
{ Public declarations }
Constructor Create (AOwner : TComponent); override;
procedure CreateParams(var Params: TCreateParams); override;
procedure SetAlignment(Value: TAlignment);
property KeyPressed : boolean read FKeyPressed write FKeyPressed default False;
property Alignment: TAlignment read FAlignment write SetAlignment default taLeftJustify;
published
{ Published declarations }
//new properties
property AutoRounding : boolean read FAutoRounding write FAutoRounding default False; //publishing properties declared in TCustomEdit
property Anchors;
property AutoSelect;
property AutoSize;
property BiDiMode;
property BorderStyle;
property CharCase;
property Color;
property Constraints;
property Ctl3D default false;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property Font;
property HideSelection;
property ImeMode;
property ImeName;
property MaxLength;
property OEMConvert;
property ParentBiDiMode;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PasswordChar;
property PopupMenu;
property ReadOnly;
property ShowHint;
property TabOrder;
property TabStop;
property Text;
property Visible;
property OnChange;
property OnClick;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
end;procedure Register;implementationConstructor TNumEdit.Create (AOwner : TComponent);
begin
Inherited Create(AOwner);
SetAlignment(taRightJustify);
BevelKind:=bkFlat;
BorderStyle:=bsNone;
Round:=true;
RoundingDecimals:=2;
Text:='0';
end;procedure TNumEdit.CreateParams(var Params: TCreateParams);
const
Alignments: array[Boolean, TAlignment] of DWORD =
((ES_LEFT, ES_RIGHT, ES_CENTER),(ES_RIGHT, ES_LEFT, ES_CENTER));
begin
inherited CreateParams(Params);
with Params do
begin
Style := Style or ES_MULTILINE or
Alignments[UseRightToLeftAlignment, FAlignment];
end;
end;procedure TNumEdit.SetAlignment(Value: TAlignment); //Set the Alignment
begin
if FAlignment <> Value then
begin
FAlignment := Value;
RecreateWnd; //Force a redraw of the control
end;
end;Function TNumEdit.RoundedText(aText : String) : String; //Perform rounding the text
begin
FmtStr(Result,'%0.'+ IntToStr(RoundingDecimals) +'f',[StrToFLoat(Text)]);
KeyPressed := false;
end;procedure TNumEdit.Change;
begin
if not KeyPressed then
begin
if (Text <> '') and Round then
Text := RoundedText(Text);
end
end;procedure TNumEdit.DoEnter; // Perform rounding when entering
begin
If AutoRounding then
if (Text <> '') and Round then
Text := RoundedText(Text);
end;procedure TNumEdit.DoExit; // Perform rounding when exiting
begin
if (Text <> '') and Round then
Text := RoundedText(Text);
end;procedure TNumEdit.KeyPress(var Key: Char);
begin
KeyPressed := true;
Inherited KeyPress(Key); // For user events //Check for Numeric and backspaces
if not (Key in ['0'..'9', '-', DecimalSeparator,#8]) then
begin
KeyPressed := False;
key:=#0;
beep;
end else //Check for existing ofdecimalsepatator and '-'
if ((Key = DecimalSeparator) or (key = '-')) and (Pos(Key, Text) > 0) then
begin
If Key = DecimalSeparator then
begin
SelStart := Pos(Key, Text);
SelLength := Length(Text);
end;
key:=#0;
end else // If '-' then first check position at front
if (Key = '-') and (Selstart <> 0) then
begin
key:=#0;
beep;
end;
end;procedure Register;
begin
RegisterComponents('ECSMS', [TNumEdit]);
end;
end.
var
r,f:real;
begin
r := 123.4566756778 ///是有一定的精度了
f := FormatFloat('0.00',r); //结果是f=123.46
end;