Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pad with 000's

Status
Not open for further replies.

MattJenneson

Programmer
Sep 12, 2002
8
GB
Hi,

I'm new to delphi and been lumbered with a project for work.

I need to pad a number with zero's.

ie. 1 becomes 0000001
2 becomes 0000002
etc

Similar to the RIGHT function in SQL.
I'm sure it's simple, i just can't find it.

Thank you.

 
I prefer using the format string function:

strPaddedNumber := Format('%-7s',[strNumber]);

Check the help on for 'Format Strings', this is a very powerful formatting tool. I use it to format fixed width text files like this:

Code:
headerstring := Format('%-2s%-4s%-10s%-4s%-4s%-12s',['D', 'MP', '218', 'MP', '218', AVoucher]);

which produces a string with the exact widths I need for my file.


Leslie
 
Actually, the correct format uses the %d descriptor as follows:

var
value: Integer;
StrFmt: String;

begin
value := 5;
StrFmt := Format('%.4d', [value]); { Will generate 0005 }
end;
 
the correct format uses the %d descriptor

How do you know that it's a number?

I showed it using a string because I can't think of a valid mathematical reason for having a number padded with '0' if you are going to use it in a calculation.

I have lots of numbers stored in my tables as strings because I don't add zipcodes or phone numbers. No reason for those to be number fields.

I also told him what to search for in the help for more detailed information because this is useful in formatting a lot of different items, currency, decimals, strings.

Leslie
 
...There's always several ways to solve problems:
Code:
var
  TheValue: Integer;
  TheString: String;
begin
  TheValue := 12;
  TheString := FormatFloat('0000000', TheValue);

//Nordlund
 
Les,

I don't think that your solution pads on the left with leading zeros. It pads on the right with spaces!

As Nordlund suggests there are lots of ways to solve this problem. Here are a pair of general purpose LeftPad functions that will cope with either integers or strings. You can include both in your source code as they are overloaded. They default to a length of 7 (as specified in the original post) and a fill character of '0' but these can be overridden.
Code:
function LeftPad(value:integer; length:integer=8; pad:char='0'): string; overload;
begin
  result := RightStr(StringOfChar(pad,length) + IntToStr(value), length );
end;

function LeftPad(value: string; length:integer=8; pad:char='0'): string; overload;
begin
  result := RightStr(StringOfChar(pad,length) + value, length );
end;

Example calls are:
Code:
var
  r: string;
  s: string;
  i: integer;
begin
  s := '1234';
  r := LeftPad(s);       // r will contain '0001234'
  r := LeftPad(s,5);     // r will contain '01234';
  r := LeftPad(s,6,'*'); // r will contain '**1234';
  i := 1234;
  r := LeftPad(i);       // r will contain '0001234'
  r := LeftPad(i,5);     // r will contain '01234';
  r := LeftPad(i,6,'*'); // r will contain '**1234';
end;


Andrew
Hampshire, UK
 
Towerbase
Neat, but I'm a bit doubtful about the backwords compatibility of your code.


Steve
Be excellent to each other and Party on!
 
Steve,

You are right - those functions won't compile on early versions of Delphi.

The RightStr function is in the StrUtils unit of Delphi 7 (maybe D6, too). I can't recall when function overloading was introduced into Delphi.

If someone doesn't specify what version of Delphi they are using then all I can assume is that it is not important - i.e. they have the latest version.

Andrew
Hampshire, UK
 
Just to confirm, RightStr is in Delphi 6 too.

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
This one has been in my utils file for quite a while
and is backwards compatable to all versions of Delphi

PadC('hello',10,'0',True);
returns '00000hello'
PadC('hello',10,'0',False);
returns 'hello00000'


Code:
function PadC(s:string; L:integer; c: char; Front: Boolean):string;
 {Pads string s with char C to lenth l}
 var p: string;
 begin
    P := stringofchar(C, L - length(S));
    if Front then
       PadC :=  p + s
    else
       PadC :=  s + p;
end;

Steve
Be excellent to each other and Party on!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top