Board index » delphi » Format parameters

Format parameters

Could somebody give me a clear example of how I would format a string to
look like a number ;-
eg 1256907  should look like 1,256,907

I cannot find a clear example in the Help files or books
Many Thanks,

Chris

 

Re:Format parameters


Quote
"chrisross.arc" <chrisross....@eggconnect.net> wrote in message

news:3c06547b$1_1@dnews...

Quote
> Could somebody give me a clear example of how I would format a string to
> look like a number ;-
> eg 1256907  should look like 1,256,907

> I cannot find a clear example in the Help files or books
> Many Thanks,

s := formatfloat('###,###,###', 1256907)

HTH
Woody

Re:Format parameters


Hi,
  If you are looking to display or edit data from a database, you can simply
use the DisplayFormat property for the field in question.  In other words,
if you have a field that is 10 digits long, create the TField object for
that field, and set it's DisplayFormat to #,###,###,###.
If you are not using a database, you could write a procedure that takes the
number (as a string) and puts the commas in.  Something like

Function AddCommas(Num: Integer): String;
Var NewNum, OldNum: String;
      Index: integer;
Begin
  try
    OldNum := IntToStr(Num);
    NewNum := '';
    For Index := 0 to Length(OldNum) - 1 do
    begin
      if ((Index + 1) mod 3 = 0) and
          ((Index + 1) <> Length(OldNum)) then
        //add comma after number only if this isn't the last number.
        NewNum := Concat(',' + Copy(OldNum, Length(OldNum) - Index, 1),
NewNum)
      else
        NewNum := Concat(Copy(OldNum, Length(OldNum) - Index, 1), NewNum)
    end;
    Result := NewNum;
  Except
    ShowMessage('Invalid Number');
  end;
End;

Hope this helps
Doug

Quote
"chrisross.arc" <chrisross....@eggconnect.net> wrote in message

news:3c06547b$1_1@dnews...
Quote
> Could somebody give me a clear example of how I would format a string to
> look like a number ;-
> eg 1256907  should look like 1,256,907

> I cannot find a clear example in the Help files or books
> Many Thanks,

> Chris

Re:Format parameters


Chris,

function FormatIntegerWithCommas(const AInteger : integer) : string;
begin
  Result := FormatFloat('#,##0', StrToFloat(IntToStr(AInteger)));
end;

HTH,

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/

Re:Format parameters


Format( '%.0n', [ 1256907.0 ] );

See " Format Strings " for examples.

Rick

Other Threads