Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 159
  • Last Modified:

Formatting a variable

Hello all

I have a string variable that gives me a result of for example '2' and i need to show this as '0002', i know it sounds really easy but i just cant get it. I need it ti be 4 characters long with 0 as fill characters whatever the variable produces

Any ideas please
0
Paul-Brooks
Asked:
Paul-Brooks
  • 4
  • 2
1 Solution
 
atul_parmarCommented:
var
  s : string;
begin
  s := '2';
  s := StringOfChar('0', 4-Length(s)) + s;
  ShowMessage(s);
end;
0
 
sun4sundayCommented:
Add the string variable to ;000'

ShowMessage('000'+'2');

result is '0002'

sun4sunday
0
 
atul_parmarCommented:
And what would happen if the string has '12', '123', or '1234' ??????????
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sun4sundayCommented:
Create a new project and place one edit box and a button

put the follwing code on the button click

procedure TForm1.Button1Click(Sender: TObject);
begin
if length(edit1.Text) = 1 then
  edit1.Text := '000' + edit1.Text
else
if length(edit1.Text) = 2 then
  edit1.Text := '00' + edit1.Text
else
if length(edit1.Text) = 3 then
  edit1.Text := '0' + edit1.Text;
end;



sun4sunday
0
 
atul_parmarCommented:
Hey, nevermind but it game me a smile :)
0
 
atul_parmarCommented:
Guys, are you using Delphi?????????????????
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now