1. Home
  2. Computing & Technology
  3. Delphi Programming

How to add Leading Zeroes to a Number (Delphi Format)

By , About.com Guide

Here's how convert (an integer) number to a string by adding an amount of leading zeroes.

Suppose you are developing a database application, and you need to operate on, let's say, a customer number, where each number needs to be exactly 10 digits "long" - but you have customer numbers of 2,4,7, etc. (<10) digits.

The AddLeadingZeroes function accepts two parameters: aNumber is the number that needs to have exactly Length characters (if less, add leading zeroes).

~~~~~~~~~~~~~~~~~~~~~~~~~
function AddLeadingZeroes(const aNumber, Length : integer) : string;
begin
   result := SysUtils.Format('%.*d', [Length, aNumber]) ;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Usage:
AddLeadingZeroes(2005, 10) ;

Will result in a '0000002005' string value.

Delphi tips navigator:
» How to get the selected text from a Memo control
« How to clear the graphics in a TImage Delphi control

More Delphi Programming Quick Tips
Explore Delphi Programming
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2005 Delphi Tips
  7. How to add Leading Zeroes to a Number (Delphi Format)

©2010 About.com, a part of The New York Times Company.

All rights reserved.