formating number

function GetFormattedNumber(Value: Double): string; 
 
  function GetLastPos(const s: string): Integer; 
  begin 
    Result := Pos(DecimalSeparator, s)-1; 
    if (Result < 1) then 
      Result := Length(s) 
  end; 
 
var 
  i: Integer; 
begin 
  Result := FloatToStr(Value); 
  i := 3; 
  while (i < GetLastPos(Result)) do 
  begin 
    Insert(' ', Result, GetLastPos(Result)-i+1); 
    Inc(i, 4) 
  end; 
end;

Sample to use:

  lblTotal.Caption := GetFormattedNumber(123456.789);

Why I wrote this function? In Delphi I could use the FormatFloat(‘,###’, dbl) but FormatFloat uses the thousand separator which is defined in MS Windows (#160 by default). I need the space char always

Tags:

Comments are closed.