Numeric to string conversion

If you don’t want to print directly but concatenate any number of variables into a complete string, Pascal offers three mainstream implementation methods:

Concatenation via Dedicated Conversion Functions

***ToStr + string + concatenation

Convert single data types separately with dedicated conversion functions, then join strings using the + operator:

uses SysUtils;

var
  MyInt: Integer;
  MyStr: string;
begin
  MyInt := 123;
  MyStr := 'My int number is ' + IntToStr(MyInt) + ', and the value of Pi is ' + FloatToStr(Pi);
end;Code language: JavaScript (javascript)

Common supporting functions (all located in the SysUtils unit):

  • Number to string: IntToStr, FloatToStr, FormatFloat (custom floating-point formatting)
  • String back to numeric value: StrToInt, StrToIntDef, StrToFloat

Fully runnable example

{$ifdef FPC} {$mode objfpc}{$H+}{$J-} {$endif}
{$ifdef MSWINDOWS} {$apptype CONSOLE} {$endif}

uses SysUtils;

var
  MyInt: Integer;
  MyStr: string;
begin
  MyInt := 180;
  MyStr := 'My int number is ' + IntToStr(MyInt) + ', and the value of Pi is ' + FloatToStr(Pi);
  WriteLn(MyStr);
end.Code language: PHP (php)
Advantages
  1. Extremely flexible; split dedicated functions cover most types including integers, floats and dates;
  2. Complete matching reverse conversion utilities, convenient for parsing input strings from users.
Disadvantages

When there are many variables, multiple stacked + operators result in verbose, hard-to-read code.


Format Function

Format (similar to sprintf in C language)

Basic Syntax

uses SysUtils;

var
  Res: string;
  MyInt: Integer;
  MyFloat: Double;
  MyString: string;
begin
  Res := Format('%d %.4f %s', [MyInt, MyFloat, MyString]);
end;Code language: JavaScript (javascript)
  • %d: integer placeholder; %.4f: floating-point number with 4 decimal places reserved; %s: string placeholder
  • The second parameter is a constant array that fills placeholders sequentially

Full Example

{$ifdef FPC} {$mode objfpc}{$H+}{$J-} {$endif}
{$ifdef MSWINDOWS} {$apptype CONSOLE} {$endif}

uses SysUtils;

var
  Res: string;
  MyInt: Integer;
  MyFloat: Double;
  MyString: string;
begin

  // set value
  MyInt := 169;
  MyFloat := 0.618;
  MyString := 'foxdevelop.com';

  Res := Format('%d %.4f %s', [MyInt, MyFloat, MyString]);
  WriteLn(Res);
end.Code language: PHP (php)

Advantages

  1. Format template separated from data for clean structure; for software multi-language translation, only template strings need modification without changing variable lists;
  2. No compiler hidden logic. Declare custom functions with array of const variable parameters to reuse this formatting logic and forward parameters to Format for native processing.

Disadvantages

The compiler does not verify matching between placeholders and variable types at compile time; mismatched types throw an EConvertError conversion exception at runtime (will not trigger memory access crashes).


WriteStr

Store Write output into a string variable

Purpose: Behaves almost identically to Write/WriteLn, but instead of printing to console, it stores output into the first target string variable:

var S: string;
begin
  WriteStr(S, 'Pi value: ', Pi:1:4);
end;Code language: JavaScript (javascript)

Full Example

{$ifdef FPC} {$mode objfpc}{$H+}{$J-} {$endif}
{$ifdef MSWINDOWS} {$apptype CONSOLE} {$endif}

uses SysUtils;

var S: string;
begin
  WriteStr(S, 'Pi value: ', Pi:1:4);
  WriteLn(S);
end.Code language: PHP (php)

Next:

Leave a Reply

Your email address will not be published. Required fields are marked *