procedure TForm.DBGridDrawDataCell(Sender: TObject; const Rect: TRect; Field: TField; State: TGridDrawState); var temp:string; begin with DBGrid.Canvas do begin FillRect(Rect); if (Field.DataType = ftFloat) then begin if not (Field.IsNull) then temp:= format('%8.1f',[strtofloat(field.asstring)]); TextOut(Rect.Right-TextWidth(temp)-3,Rect.Top+3, temp); end else TextOut(Rect.Left+2,Rect.Top+3,Field.AsString); end; end;
2、设置小数位数
3、取消默认值这样才能保存真实的录入数据剩下的采用DisplayFormat:='0.00'的方式,这样只是规范显示,用0补齐显示
Field: TField; State: TGridDrawState);
var
temp:string;
begin
with DBGrid.Canvas do
begin
FillRect(Rect);
if (Field.DataType = ftFloat) then
begin
if not (Field.IsNull) then temp:= format('%8.1f',[strtofloat(field.asstring)]);
TextOut(Rect.Right-TextWidth(temp)-3,Rect.Top+3, temp);
end
else TextOut(Rect.Left+2,Rect.Top+3,Field.AsString);
end;
end;
比较好的办法就是用定点数格式的小数,这样没有误差,速度也非常快。