要使小数点左边固定某长度(如5位),小数点右边保留2位,要使用哪个格式化函数?
如:
2.1
3333.2格式化后变成:
____2.10
_3333.20再次,暂用“_”表示空格符

解决方案 »

  1.   

    var   
      s1:string;   
      ...   
      formatfloat('0.00',strtofloat(s1)) 
    uses SysUtils
    或者uses Math,用roundto
      

  2.   

    那就用Insert插入空格和for循环,用Length求长度,然后5-length,剩下的insert 空格
      

  3.   

    我的目的是通过对下面语句进行循环,将表中所有记录的“总金额”字段打印出来,要求小数点对齐,即,小数点左边不足5位时,用空格补,小数点右边如果只有1位时,第2位用零补。
    但是,循环执行下面语句后是左对齐的,不知道怎么格式化,拜托问问大家,谢谢!!!
    Printer.Canvas.TextOut(x+3071,y,'┃'+FormatFloat('0.00',ADOQuery1.FieldByName('总金额').AsFloat));
      

  4.   

    function FormatString(你的串):string;
    begin
    if 能找到'.' then
    begin
      sub1 : = .之前;
      sub2 := .之后;
      Result := LeadingSpace(sub1,5) + '.' +AddZero(sub2,2);  
    end
    else
    begin
      Result := LeadingSpace(你的串,5) + '.00' 
    end;
    end;
    function LeadingSpace(.之前串,位数):string;
    begin
     while Length(Result)<位数 do Result:=' '+Result;
    end;
    function AddZero(.之后串,位数):string;
    begin
     while Length(Result)<位数 do Result:=Result+'0';
    end;
      

  5.   

    一句话:Format('%8.2f',[value]);
    value是你的值,必须为数值型,一般用Real型。
      

  6.   

    我参考网上资料写了一个在字符串前头加空格的函数:
    unit my_func;interfacefunction add_space(from_str:string):string;implementationfunction add_space(from_str:string):string;
    var
      TempStr:String;
      i:integer;
    begin
          TempStr:=from_str;         //from_str为需要格式的字符串   
          if length(TempStr)<8 then   //假设定长为8(小数点之前5为,小数点之后2位)
          begin   
              for i:=1 to 8-Length(TempStr) do   
              begin   
                  TempStr:=' '+TempStr;   
              end;   
          end;   
          from_str:=TempStr;   
    end;end.
    这样调用:
    add_space(FormatFloat('0.00',12));跟踪函数执行过程,在循环里运行都是正常的(能在字符串加上前头补上前导空格),但是返回值却是一个空格,哪里出错了?
      

  7.   

    这是我写的一个函数,使用它既可搞定!!!!!
    function FormatFloatS(Value:Double;
                          HeadLen,//头长度
                          EndLen:integer//尾长度
                          ):String;
    var
      m,heads,ends:String;
      i,k:integer;
    begin
      m:=FormatFloat('#.#',Value);
      i:=pos('.',m);
      if i>0 then
      begin
        heads:=Copy(m,1,i-1);
        ends:=Copy(m,i+1,length(m)-i);
        if length(heads)<= HeadLen then
        begin
          while length(heads)<headLen do
          begin
            heads:='0'+heads;
          end;
        end
        else
        begin
          heads:=Copy(heads, HeadLen-5,HeadLen);
        end;
        if length(ends)<= EndLen then
        begin
          while length(ends)< EndLen  do
          begin
            ends:= ends+'0';
          end;
        end
        else
        begin
          heads:=Copy(heads,1,HeadLen);
        end;
     end
     else
     begin
        heads:=m;
        if length(heads)<= HeadLen then
        begin
          while length(heads)<headLen do
          begin
            heads:='0'+heads;
          end;
        end
        else
        begin
          heads:=Copy(heads, HeadLen-5,HeadLen);
        end;
        ends:='';
        while length(ends)< EndLen  do
        begin
          ends:= ends+'0';
        end;
     end;
     Result:=heads+'.'+ends;
    end;
      

  8.   

    奇怪,上面函数在循环里TempStr的值是正常的,循环结束怎么就变成一个空格了