开发者

How to keep 2 decimal places in Delphi?

I have selected columns from a database table and want this data with two decimal places only. I have:

SQL.开发者_JAVA技巧Strings = ('select '#9'my_index '#9'his_index,'...
  • What is that #9?
  • How can I deal with the data I selected to make it only keep two decimal places?

I am very new to Delphi.


#9 is the character with code 9, TAB.

If you want to convert a floating point value to a string with 2 decimal places you use one of the formatting functions, e.g. Format():

var
  d: Double;
  s: string;
...
d := Sqrt(2.0);
s := Format('%.2f', [d]);


function Round2(aValue:double):double;    
begin    
  Round2:=Round(aValue*100)/100;    
end;


#9 is the tab character.

If f is a floating-point variable, you can do FormatFloat('#.##', f) to obtain a string representation of f with no more than 2 decimals.


For N Places behind the seperator use

function round_n(f:double; n:nativeint):double;
var i,m : nativeint;
begin
  m := 10;
  for i := 1 to pred(n) do
    m := m * 10;

  f := f * m;
  f := round(f);
  result := f / m;      
end;


For Float to Float (with 2 decimal places, say) rounding check this from documentation. Gives sufficient examples too. It uses banker's rounding.

x := RoundTo(1.235, -2); //gives 1.24

Note that there is a difference between simply truncating to two decimal places (like in Format()), rounding to integer, and rounding to float.


Nowadays the SysUtils unit contains the solution:

System.SysUtils.FloatToStrF( singleValue, 7, ffFixed, 2 );
System.SysUtils.FloatToStrF( doubleValue, 15, ffFixed, 2 );

You can pass +1 TFormatSettings parameter if the requiered decimal/thousand separator differ from the current system locale settings.


The internal float format routines only work with simple numbers > 1

You need to do something more complicated for a general purpose decimal place limiter that works correctly on both fixed point and values < 1 with scientific notation.

I use this routine

function TForm1.Flt2str(Avalue:double; ADigits:integer):string;
var v:double; p:integer; e:string;
begin
  if abs(Avalue)<1 then
  begin
    result:=floatTostr(Avalue);
    p:=pos('E',result);
    if p>0 then
    begin
      e:=copy(result,p,length(result));
      setlength(result,p-1);
      v:=RoundTo(StrToFloat(result),-Adigits);
      result:=FloatToStr(v)+e;
    end else
      result:=FloatToStr(RoundTo(Avalue,-Adigits));
  end
  else
    result:=FloatToStr(RoundTo(Avalue,-Adigits));
end;

So, with digits=2, 1.2349 rounds to 1.23 and 1.2349E-17 rounds to 1.23E-17


This worked for me :

Function RoundingUserDefineDecaimalPart(FloatNum: Double; NoOfDecPart: integer): Double;
Var
     ls_FloatNumber: String;
Begin
     ls_FloatNumber := FloatToStr(FloatNum);
     IF Pos('.', ls_FloatNumber) > 0 Then
          Result := StrToFloat
            (copy(ls_FloatNumber, 1, Pos('.', ls_FloatNumber) - 1) + '.' + copy
                 (ls_FloatNumber, Pos('.', ls_FloatNumber) + 1, NoOfDecPart))
     Else
          Result := FloatNum;
End;


Function RealFormat(FloatNum: Double): string;
Var
     ls_FloatNumber: String;
Begin
     ls_FloatNumber:=StringReplace(FloatToStr(FloatNum),',','.',[rfReplaceAll]);
     IF Pos('.', ls_FloatNumber) > 0 Then
          Result :=
            (copy(ls_FloatNumber, 1, Pos('.', ls_FloatNumber) - 1) + '.' + copy
                 (ls_FloatNumber, Pos('.', ls_FloatNumber) + 1, 2))
     Else
          Result := FloatToStr(FloatNum);
End;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜