Delphi DBGrid 固定小數位數的实现(形如1,000,000.00)

在DBGrid的DBGridDrawColumnCell事件中嵌入如下代码:

var
 strShow: string;
begin
  if (Column.FieldName='lastyear') or (Column.FieldName='thisyear') then              //lastyear和thisyear为需要格式化输出的数据表中的字段。
    begin
      if not Column.Field.IsNull then
        strShow := FormatFloat(',0.00', Column.Field.AsFloat) //',0.00'是显示格式。
      else
        strShow := '';
      Column.Field.Alignment := taRightJustify;
      if Rect.Right - Rect.Left < dbgrid1.Canvas.TextWidth(strShow) then
        strShow := Copy(strShow, 1, Trunc(Length(strShow) * (Rect.Right - Rect.Left)
          / (dbgrid1.Canvas.TextWidth(strShow) + 2)));
      dbgrid1.Canvas.FillRect(Rect);
      dbgrid1.Canvas.TextOut(Rect.Right - dbgrid1.Canvas.TextWidth(strShow) - 2,
        Rect.Top + 2, strShow);
      Exit;
    end;
end;

你可能感兴趣的:(Delphi)