我从数据库中导出数据到记事本中,最后导出的格式如下
麻酱油麦菜                8          
鲜辣凤爪                24                          
麻辣牛肉                24                             
肘花                187   
但是我需要的格式如下,也就是价格字段要右对齐,大家帮我看看如何实现好吗
麻酱油麦菜               8.00          
鲜辣凤爪                24.00                          
麻辣牛肉                24.00                             
肘花                   187.00   

解决方案 »

  1.   

    implementation
    uses StrUtils;{引用这个单元,因为下面代码中用到一个函数:DupeString}
    {$R *.dfm}type
      TMyRec = record{以下定义一个记录类型,并且声明一个记录数组,代替你的数据库数据,便于测试}
        Name: string;
        Price: Double;
      end;var
      ArrRec: array[1..4] of TMyRec
        = ((Name:'麻酱油麦菜';Price:  8.0),
           (Name:'鲜辣凤爪';  Price: 24.0),
           (Name:'麻辣牛肉';  Price: 24.0),
           (Name:'肘花';      Price:187.0));procedure TForm1.Button1Click(Sender: TObject);
    var
      Len, I: integer;
      F : TextFile;
      S1, S : string;
    begin
      Len := 20; {假设需要每行固定20个Char长度}
      AssignFile(F, 'c:\test.txt');
      ReWrite(F);
      try
        for I := Low(ArrRec) to High(ArrRec) do begin
          S1 := Format('%.2f', [ArrRec[I].Price]);
          S := ArrRec[I].Name + DupeString(#32, Len-Length(ArrRec[I].Name)-Length(S1)) + S1;
          WriteLn(F, S);
        end;
      finally
        CloseFile(F);
      end;
    end;
      

  2.   


    请仔细看我代码中的这一句:
    S1 := Format('%.2f', [ArrRec[I].Price]);这就是利用Format函数,实现了你的需求.
    当然,如果你的数字本来是2位以上小数,为了精度,应进行四舍五入运算.
      

  3.   

    续三楼:Format函数已经带有四舍五入了,请运行以下测试代码看结果:
    procedure TForm1.Button2Click(Sender: TObject);
    var
      D: double;
      S: string;
    begin
      D := 3.878;
      S := Format('%.2f', [D]);
      Showmessage(S);{3.88}  D := 99;
      S := Format('%.2f', [D]);
      Showmessage(S);{99.00}  D := 99.996;
      S := Format('%.2f', [D]);
      Showmessage(S);{100.00}
    end;
      

  4.   


    procedure TForm1.Button2Click(Sender: TObject);
    var
      I: integer;  {如果原数是整数}
      intS: string;{或者整数字符串}
      S: string;
    begin
      I := 187; {整数}
      S := Format('%.2f', [StrtoFloat(InttoStr(I))]); {两次转换}
      Showmessage(S);  intS := '187'; {整数字符串}
      S := Format('%.2f', [StrtoFloat(intS)]); {一次转换}
      Showmessage(S);
    end;
      

  5.   

    今日算是遇到高人了,谢谢高手悉心指点。我还有个问题想请教,var
      ArrRec: array[1..4] of TMyRec
        = ((Name:'麻酱油麦菜';Price:  8.0),
           (Name:'鲜辣凤爪';  Price: 24.0),
           (Name:'麻辣牛肉';  Price: 24.0),
           (Name:'肘花';      Price:187.0));以上代码我是这样写的,可是有错误,错误信息一是'ItemList_Price'(价格)字段的类型错误,二是'TMyRec and string' 。帮我再看看好吗,谢谢!!!!!!!1
    {$R *.dfm}Var
        ItemListSortId:string;
        ItemListId:String;
        ItemListName: string;
        ItemListPrice: String;
        ItemListUnit:String;Var
    ArrRec: array of TMyRec ;
    begin
    ...................
    for   I:=0   to   OraQuery1.FieldCount-1 do
    begin
       ArrRec[I]:= (ItemListSortId+':'+OraQuery1.fieldbyname('ItemList_SortId').AsString)+';'
       +ItemListId+':'+OraQuery1.fieldbyname('ItemList_Id').AsString+';'
       +ItemListName+':'+OraQuery1.fieldbyname('ItemList_Name').AsString+';'
       +(ItemListPrice)+':'+OraQuery1.fieldbyname('ItemList_Price').asstring);
    ..........................
    end;
      

  6.   

    也就是说,对于下面这部分:type
      TMyRec = record{这就相当于你的表的结构}
        Name: string;
        Price: Double;
      end;var
      ArrRec: array[1..4] of TMyRec   {这就相当于你的表的内容}
        = ((Name:'麻酱油麦菜';Price:  8.0), {这相当于你的表的第一条记录}
           (Name:'鲜辣凤爪';  Price: 24.0),{这相当于你的表的第二条记录}
           (Name:'麻辣牛肉';  Price: 24.0),{这相当于你的表的第三条记录}
           (Name:'肘花';      Price:187.0));{这相当于你的表的第四条记录}
    明白了吗?因为对我来讲,不可能为了解答题目而新建一个数据库,建每个字段,添加一些记录-----这太麻烦-----所以我用了上面这种方式代替数据表.
      

  7.   


    郁闷.....对于一楼代码,你需要这样改代码:
        ADOQuery1.First;{指向第一条记录}
        while Not ADOQuery1.Eof do begin
          S1 := Format('%.2f', [价格字段取值]);{如值不为实型,请参照5楼转换}
          S := 名称字段值 + DupeString(#32, Len-Length(名称字段值)-Length(S1)) + S1;
          WriteLn(F, S);
          ADOQuery1.Next;{加这一句,每循环一次指向下一条记录}
        end;
    这也只是一个意思,看你自己了. 不可能手把手教的.
    (看你在最初提问题时已经可以导出数据了, 只是格式不会对齐而已)