delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback



Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi








Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (2)


Simple Way To Give Leading Zero In a NumberFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
Others
Skill Level:
Scoring:
Last Update:
01/21/2007
Search Keys:
delphi delphi3000 article borland vcl code-snippet Leading-Zero Format String
Times Scored:
2
Visits:
2662
Uploader: Hary Purnomo
Company:
Reference: http://delphimiracles.blogspot.com/
 
Question/Problem/Abstract:
How can we put the leading zero in a Number with specifix n Max Digit place holder ??
Is there any Delphi built in function to do this job ??

Example:
» if digit place holder is 5 => 00123
» if digit place holder is 3 => 012
Answer:



Actually, Delphi has supported to do this kind of job.
Yes, we can use function "Format" strings to do this
But we must do a little trick here to use it appropriately

Here is my example code:
********************************

function GiveLeadingZero(const aNumber, aMaxDigit: Integer): String;
var formatSpecifier: String;
begin
  formatSpecifier := Format('%%.%dd', [aMaxDigit]);

  // formatSpecifier will result like this: '%.5d' if aMaxDigit=5
  Result := Format(formatSpecifier, [aNumber]);
end;

procedure TForm1.Button1Click(Sender: TObject);
var aNumber: Integer;
begin
  if (TryStrToInt(Edit1.Text, aNumber)) then
    Edit2.Text := GiveLeadingZero(aNumber, 5)
  else
    ShowMessage('Value in Edit1 is not a valid integer value');
end;

********************************

To use those code, simply juzt put two TEdit component on the Form with one button to execute the code



I think it solved the problem.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Using FormatFloat()
    Mike Heydon (Nov 24 2006 8:28AM)

You can achieve the same result by using the FormatFloat() function. Despite it's name it will take integer types as arguments.

function GiveLeadingZero(const aNumber, aMaxDigit: Integer): String;
begin
    Result := FormatFloat(StringOfChar('0',aMaxDigit),aNumber);
end;

Respond

RE: Using FormatFloat()
Hary Purnomo (Nov 24 2006 6:59PM)

o, i see
yes, it will produce the same result
thanks for your sharing here
its never thought by me until now :D
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


   


  Community Ad of
E. Irigoyen
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)