Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,624 questions

56,911 answers

573 users

How to format a string using fixed-width fields in Pascal

3 Answers

0 votes
(*
Common format specifiers
-----------------------------------
%-10s { Left-aligned string, width 10 }
%10s { Right-aligned string, width 10 }
%5d { Integer, width 5 }
%08d { Integer, width 8, padded with zeros }
%12.2f { Float, width 12, 2 decimal places }
*)

program FixedWidthFields;

{$mode objfpc}{$H+}

uses
  SysUtils;

{ Formats a record using fixed-width fields }
function FormatRecord(const Name: string; Age: Integer;
  Salary: Double): string;
begin
  {
    %-15s = left-aligned string, width 15
    %10d  = right-aligned integer, width 10
    %12.2f = floating-point value, width 12, 2 decimal places
  }
  Result := Format('%-15s%10d%12.2f',
    [Name, Age, Salary]);
end;

var
  Output: string;

begin
  { Store the formatted string in a variable }
  Output := FormatRecord('R2D2', 96, 185000.0);

  WriteLn(Output);
end.



{
run:

R2D2                   96   185000.00

}

 



answered 3 days ago by avibootz
edited 3 days ago by avibootz
0 votes
program FixedWidthString;

{$mode objfpc}{$H+}

uses
  SysUtils;

var
  ResultStr: string;

begin
  { Left-align "Hello" in a field 10 characters wide }
  ResultStr := Format('%-10s', ['Hello']);

  WriteLn('|', ResultStr, '|');
end.


{
run:

|Hello     |

}

 



answered 3 days ago by avibootz
0 votes
program FixedWidthName;

{$mode objfpc}{$H+}

uses
  SysUtils;

var
  NameField: string;

begin
  { Right-aligned in a field of width 10 }
  NameField := Format('%10s', ['Orion']);

  WriteLn('|', NameField, '|');
end.


{
run:

|     Orion|

}

 



answered 3 days ago by avibootz
...