4 8 Votes

Delphi: Add leading zeros to a number

Tip by Progger99 | 14/08/2012 at 09:48

Sometimes we want to represent a number with leading zeros when using Delphi, for example, if the numbers become double digits at some point and we directly want to start at 01, 02 and so on. Fortunately there is a simple Delphi function which can do this for us:

str := Format('%.*d',[length, number]);
str := Format('%.*d',[3, 7]);
//str is '007'

The two required parameters are the number of places that we want to have (length) and the integer, we want to rewrite (number). The return value is a string.

In the second line you can see an example: When we call the function with the parameters 3 and 7, we get '007'.

ReplyPositiveNegative

About the Author

Avatar AuthorThe author has not added a short description to his profile yet.
Show Profile | Message

 

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own articles on askingbox.com. How to do.