Retourner en haut
DELPHI TUTORIEL :
CONVERSION DES VALEURS EN CHAÎNES DE CARACTÈRES

Vous trouverez sur cette page des exemples. Comment convertir des nombres entier et des nombres réel en chaînes de caractères? Comment formater des nombres entier et des nombres réel en chaînes de caractères? Comment formater les valeurs d'un TDateTime ou TDate en chaînes de caractères?

CONVERSION DE NOMBRES EN CHAÎNES :
  • CurrToStrF Convertir une valeur monétaire en une chaîne avec mise en forme.
  • DateTimeToStr Convertir les valeurs de date et d'heure en une chaîne de caractères.
  • DateTimeToString Formatage riche d'une variable TDateTime dans une chaîne de caractères.
  • DateToStr Convertir les valeurs de date d'un TDateTime en une chaîne de caractères.
  • FloatToStr Convertir une valeur à virgule flottante en chaîne de caractères.
  • FloatToStrF Formatage d'une valeur à virgule flottante en chaîne de caractères
  • Format Formatage riche des nombres et du texte dans une chaîne de caractères.
  • FormatCurr Formatage riche d'une valeur monétaire dans une chaîne de caractères.
  • FormatDateTime Formatage riche d'une variable TDateTime dans une chaîne de caractères.
  • FormatFloat Formatage riche d'un nombre à virgule flottante dans une chaîne de caractères.
  • IntToHex Convertir un nombre en une chaîne contenant la représentation hexadécimale.
  • IntToStr Convertir un entier en une chaîne de caractères.
  • Str Convertir une expression de type entier ou réel en chaîne de caractères.
CurrToStrF :

CurrToStrF : Convertir une valeur monétaire en une chaîne avec mise en forme.

function CurrToStrF ( Value : Currency; Format : TFloatFormat; Digits : Integer ) : string;
function CurrToStrF ( Value : Currency; Format : TFloatFormat; Digits : Integer; const FormatSettings : TFormatSettings ) : string;
TFloatFormat = (ffGeneral, ffExponent, ffFixed, ffNumber, ffCurrency);
Exemple :
procedure TForm1.Button1Click(Sender: TObject);
var montant : Currency;
begin
  montant := 8628.428;
  // Affiche le montant format Devise
  ShowMessage('Utilisation à 4 chiffres : '+CurrToStrF(montant, ffCurrency, 4));
  // Affiche : Utilisation à 4 chiffres : 8628,4280 €
  ShowMessage('Utilisation à 2 chiffres : '+CurrToStrF(montant, ffCurrency, 2));
  // Affiche : Utilisation à 2 chiffres : 8628,43 €
  ShowMessage('Pas de virgule : '+CurrToStrF(montant, ffCurrency, 0));
  // Affiche : Pas de virgule : 8628 €
end;

DateTimeToStr :

DateTimeToStr : Convertir les valeurs de date et d'heure d'un TDateTime en une chaîne de caractères.

function DateTimeToStr ( DateTime : TDateTime ) : string;
function DateTimeToStr ( DateTime : TDateTime; const FormatSettings : TFormatSettings ) : string;
Exemple :
procedure TForm1.Button1Click(Sender: TObject);
var
  laDate : TDateTime;
begin
  laDate := now; // récupere la date et heure du jour
  ShowMessage('Date et heure du jour : '+DateTimeToStr(laDate));
  // Affiche la date et heure du jour
  laDate := StrToDateTime('24/06/2022 16:27');
  ShowMessage('Date et heure du jour : '+DateTimeToStr(laDate));
  // Affiche 24/06/2022 16:27:00 
end;

DateTimeToString :

DateTimeToString : Formatage riche d'une variable TDateTime dans une chaîne de caractères.

procedure DateTimeToString ( var Result : string; const Formatting : string; DateTime : TDateTime ) ;
procedure DateTimeToString ( var Result : string; const Formatting : string; DateTime : TDateTime; const FormatSettings : TFormatSettings ) ;
Exemple :
procedure TForm1.Button1Click(Sender: TObject);
var
  uneDate : TDateTime;
  formatdeDateTime : string;

begin
  //Configure notre variable TDateTime avec une date et une heure complètes :
  uneDate := StrToDateTime('24/06/2022 17:28');

  // Date uniquement - valeurs numériques sans zéros non significatifs (sauf l'année)
  DateTimeToString(formatdeDateTime, 'd/m/y', uneDate);
  ShowMessage('date/mois/année = '+formatdeDateTime); // Affiche jour/mois/année = 24/6/22

  // Date uniquement - valeurs numériques avec des zéros non significatifs
  DateTimeToString(formatdeDateTime, 'dd/mm/yy', uneDate);
  ShowMessage('date/mois/année = '+formatdeDateTime); // Affiche jour/mois/année = 24/06/22

  // Utilise des noms courts pour le jour, le mois et l'année
  DateTimeToString(formatdeDateTime, 'ddd d mmm yyyy', uneDate);
  ShowMessage('jour court, date, mois, année, : '+formatdeDateTime); // Affiche : jour court, mois, année = ven. 24 juin 2022

  // Utilise des noms longs pour le jour et le mois
  DateTimeToString(formatdeDateTime, 'dddd d mmmm yyyy', uneDate);
  ShowMessage('jour long, date, mois, année = '+formatdeDateTime);  // // Affiche : jour long, mois, année = vendredi 24 juin 2022

  // Utilise uniquement les paramètres ShortDateFormat
  DateTimeToString(formatdeDateTime, 'ddddd', uneDate);
  ShowMessage('ddddd : '+formatdeDateTime);  // Affiche : ddddd : 24/06/22

  // Utilise uniquement les paramètres LongDateFormat
  DateTimeToString(formatdeDateTime, 'dddddd', uneDate);
  ShowMessage('dddddd : '+formatdeDateTime); // Affiche : dddddd :  vendredi 24 juin 2022

  // Utilise les paramètres ShortDateFormat + LongTimeFormat
  DateTimeToString(formatdeDateTime, 'c', uneDate);
  ShowMessage('c : '+formatdeDateTime); // Affiche : c : 24/06/2022 17:28:00
  
  // Renvoie la date
  DateTimeToString(formatdeDateTime, 'd', uneDate); // remplacer 'd' par 'dd' pour afficher 01 02 03 etc...
  ShowMessage('date : '+formatdeDateTime); // Affiche : date : 24
end;

  // Renvoie le mois
  DateTimeToString(formatdeDateTime, 'm', uneDate); // remplacer 'm' par 'mm' pour afficher 06
  ShowMessage('mois : '+formatdeDateTime); // Affiche : m : 6
  
  // Renvoie l'année
  DateTimeToString(formatdeDateTime, 'y', uneDate); // remplacer 'y' par 'yyyy' pour afficher 2022
  ShowMessage('année : '+formatdeDateTime); // Affiche : m : 22

DateToStr :

DateToStr : Convertir les valeurs de date d'un TDateTime en une chaîne de caractères.

function DateToStr ( Date : TDateTime ) : string;
function DateToStr ( Date : TDateTime; const FormatSettings : TFormatSettings ) : string;
Exemple :
procedure TForm1.Button1Click(Sender: TObject);
var LaDate : TDateTime;
begin
LaDate := StrToDate('25/06/2022');
Label1.Caption := 'La date : ' +DateToStr(LaDate);
// Label1.Caption = La date : 25/06/2022
end;

FloatToStr :

FloatToStr : Convertir une valeur à virgule flottante en chaîne de caractères.

function FloatToStr(Value: Extended): string;
function FloatToStr(Value: Extended; const AFormatSettings: TFormatSettings): string;
Exemple :
procedure TForm1.Button1Click(Sender: TObject);
var NombreDecimal : Extended;
begin
NombreDecimal := 1234567890.123456789;
Label1.Caption := 'NombreDecimal : ' +FloatToStr(NombreDecimal);
// Label1.Caption = NombreDecimal : 1234567890.12346
end;

FloatToStrF :

FloatToStrF : Convertir une valeur virgule flottante en chaîne de caractères à l'aide des paramètres Format, Precision et Digits.

function FloatToStrF(Value: Extended; Format: TFloatFormat; Precision, Digits: Integer): string;
function FloatToStrF(Value: Extended; Format: TFloatFormat; Precision, Digits: Integer; const AFormatSettings: TFormatSettings): string;
TFloatFormat = (ffGeneral, ffExponent, ffFixed, ffNumber, ffCurrency);
Exemple :
procedure TForm1.Button1Click(Sender: TObject);
var NombreDecimal : Extended;
begin
  NombreDecimal := 1234567890.123456789;

  ShowMessage('Utilisant ffGeneral, 20,4 : '+FloatToStrF(NombreDecimal, ffGeneral, 20, 4));
  // Affiche = Utilisant ffGeneral, 20,4 : 1234567890,12345679
  ShowMessage('Utilisant ffExponent 20,4 : '+FloatToStrF(NombreDecimal, ffExponent, 20, 4));
  // Affiche = Utilisant ffExponent 20,4 : 1,23456789012345679E+0009
  ShowMessage('Utilisant ffFixed 20,4 : '+FloatToStrF(NombreDecimal, ffFixed, 20, 4));
  // Affiche = Utilisant ffFixed 20,4 : 1234567890,1235
  ShowMessage('Utilisant ffNumber 20,4 : '+FloatToStrF(NombreDecimal, ffNumber, 20, 4));
  // Affiche = Utilisant ffNumber 20,4 : 1 234 567 890,1235
  ShowMessage('Utilisant ffCurrency 20,4 : '+FloatToStrF(NombreDecimal, ffCurrency, 20, 4));
  // Affiche = Utilisant ffCurrency 20,4 : = 1 234 567 890,1235 €
end;

Format :

Format : Formatage riche des nombres et du texte dans une chaîne de caractères.

function Format(const Format: string; const Args: array of const): string;
function Format(const Format: string; const Args: array of const; const AFormatSettings: TFormatSettings): string;
Exemple :
procedure TForm1.Button1Click(Sender: TObject);
begin
 Label1.Caption := Format('Renvoie : %d', [625]); // Entier signé
 // Label1.Caption = Renvoie : 625
 Label1.Caption := Format('Renvoie : %d', [-625]); // Entier signé
 // Label1.Caption = Renvoie : -625
 Label1.Caption := Format('Renvoie : %.8d', [625]); // Entier signé
 // Label1.Caption = Renvoie : 00000625
 Label1.Caption := Format('Renvoie : %u', [625]); // Entier non signé
 // Label1.Caption = Renvoie : 625
 Label1.Caption := Format('Renvoie : %.8u', [625]); // Entier non signé
 // Label1.Caption = Renvoie : 00000625
 Label1.Caption := Format('Renvoie : %e', [12345.678]); // Réel signé
 // Label1.Caption = Renvoie : 1,23456780000000E+004
 Label1.Caption := Format('Renvoie : %f', [12345.678]); // Réel signé
 // Label1.Caption = Renvoie : 12345,68
 Label1.Caption := Format('Renvoie : %.4f', [12345.123456]); // Réel signé
 // Label1.Caption = Renvoie : 12345,1235
 Label1.Caption := Format('Renvoie : %.0f', [12345.678]); // Réel signé
 // Label1.Caption = Renvoie : 12346
 Label1.Caption := Format('Renvoie : %g', [12345.678]); // Réel signé
 // Label1.Caption = Renvoie : 12345,678
 Label1.Caption := Format('Renvoie : %n', [12345.678]); // Réel signé
 // Label1.Caption = Renvoie : 12 345,68
 Label1.Caption := Format('Renvoie : %.0n', [123456789/1]); // Réel signé
 // Label1.Caption = Renvoie : 123 456 789
 Label1.Caption := Format('Renvoie : %m', [12345.678]); // Réel signé
 // Label1.Caption = Renvoie : 12 345,68 €
 Label1.Caption := Format('Renvoie : %p', [addr(text)]); // pointeur
 // Label1.Caption = Renvoie : 0019F35C
 Label1.Caption := Format('Nom : %s - Prénom : %s', ['Dupont', 'Gerard']); // Texte
 // Label1.Caption = Nom : Dupont - Prénom : Gerard
 Label1.Caption := Format('Hexadécimal : %x', [140]); // Hexadécimal
 // Label1.Caption = Hexadécimal : 8C
end;

FormatCurr :

FormatCurr : Formatage riche d'une valeur monétaire dans une chaîne de caractères.

function FormatCurr(const Format: string; Value: Currency): string;
function FormatCurr(const Format: string; Value: Currency; const AFormatSettings: TFormatSettings): string;
Exemple :
procedure TForm1.Button1Click(Sender: TObject);
var montant : Currency;
begin
montant := 8628.428;

 // Arrondir la valeur décimale
 Label1.Caption := '##### : '+FormatCurr('#####', montant);
 // Label1.Caption = ##### : 8628
 Label1.Caption := '00000 : '+FormatCurr('00000', montant);
 // Label1.Caption = 00000 : 08628
 
 // Inclure la valeur décimale
 Label1.Caption := '0.#### : '+FormatCurr('0.####', montant);
 // Label1.Caption = 0.#### : 8628,428
 Label1.Caption := '0.## : '+FormatCurr('0.##', montant);
 // Label1.Caption = 0.## : 8628,43
 Label1.Caption := '0.0000 : '+FormatCurr('0.0000', montant);
 // Label1.Caption = 0.0000 : 8628,4280
 Label1.Caption := '0.00 : '+FormatCurr('0.00', montant);
 // Label1.Caption = 0.00 : 8628,43
 Label1.Caption := '0.#### : '+FormatCurr('0.####', -montant);
 // Label1.Caption = 0.#### : -8628,428
 
 // Format scientifique
 Label1.Caption := '0.0000000E+00 : '+FormatCurr('0.0000000E+00', montant);
 // Label1.Caption = 0.0000000E+00 : 8,6284280E+03
 Label1.Caption := '#.#######E-## : '+FormatCurr('#.#######E-##', montant);
 // Label1.Caption = #.#######E-## : 8,628428E3
 
end;

FormatDateTime :

FormatDateTime : Formatage riche d'une variable TDateTime dans une chaîne de caractères.

function FormatDateTime(const Format: string; DateTime: TDateTime): string;
function FormatDateTime(const Format: string; DateTime: TDateTime; const AFormatSettings: TFormatSettings): string;
Exemple :
procedure TForm1.Button1Click(Sender: TObject);
var uneDate : TDateTime;
begin
uneDate := StrToDateTime('24/06/2022 17:28');
 Label1.Caption := 'La date : '+ FormatDateTime('d/m/y', uneDate);
 // Label1.Caption = La date : 24/6/22
 Label1.Caption := 'La date : '+ FormatDateTime('dd/mm/yy', uneDate);
 // Label1.Caption = La date : 24/06/22
 Label1.Caption := 'La date : '+ FormatDateTime('ddd d mmm yyyy', uneDate);
 // Label1.Caption = La date : ven. 24 juin 2022
 Label1.Caption := 'La date : '+ FormatDateTime('dddd d mmmm yyyy', uneDate);
 // Label1.Caption = La date : La date : vendredi 24 juin 2022
 Label1.Caption := 'La date : '+ FormatDateTime('ddddd', uneDate);
 // Label1.Caption = La date : La date : 24/06/2022
 Label1.Caption := 'La date : '+ FormatDateTime('ddddd', uneDate);
 // Label1.Caption = La date : 24/06/2022
 Label1.Caption := 'La date : '+ FormatDateTime('dddddd', uneDate);
 // Label1.Caption = La date : vendredi 24 juin 2022
 Label1.Caption := 'La date : '+ FormatDateTime('c', uneDate);
 // Label1.Caption = La date : 24/06/2022 17:28:00
end;

FormatFloat :

FormatFloat : Formatage riche d'un nombre à virgule flottante dans une chaîne de caractères.

function FormatFloat(const Format: string; Value: Extended): string;
function FormatFloat(const Format: string; Value: Extended; const AFormatSettings: TFormatSettings): string;
Exemple :
procedure TForm1.Button1Click(Sender: TObject);
var flotteur : extended;
begin
flotteur := 8628.428;

 // Arrondir la valeur décimale
 Label1.Caption := '##### : '+FormatFloat('#####',flotteur);
 // Label1.Caption = ##### : 8628
 Label1.Caption := '00000 : '+FormatFloat('00000', flotteur);
 // Label1.Caption = 00000 : 08628
 
 // Inclure la valeur décimale
 Label1.Caption := '0.#### : '+FormatFloat('0.####', flotteur);
 // Label1.Caption = 0.#### : 8628,428
 Label1.Caption := '0.## : '+FormatFloat('0.##', flotteur);
 // Label1.Caption = 0.## : 8628,43
 Label1.Caption := '0.0000 : '+FormatFloat('0.0000', flotteur);
 // Label1.Caption = 0.0000 : 8628,4280
 Label1.Caption := '0.00 : '+FormatFloat('0.00', flotteur);
 // Label1.Caption = 0.00 : 8628,43
 Label1.Caption := '0.#### : '+FormatFloat('0.####', -flotteur);
 // Label1.Caption = 0.#### : -8628,428
 
 // Format scientifique
 Label1.Caption := '0.0000000E+00 : '+FormatFloat('0.0000000E+00', flotteur);
 // Label1.Caption = 0.0000000E+00 : 8,6284280E+03
 Label1.Caption := '#.#######E-## : '+FormatFloat('#.#######E-##', flotteur);
 // Label1.Caption = #.#######E-## : 8,628428E3
 
end;

IntToHex :

IntToHex : Convertit un nombre en une chaîne contenant la représentation hexadécimale (base 16) de ce nombre. Valeur est le nombre à convertir. Digits indique le nombre minimum de chiffres hexadécimaux à renvoyer.

function IntToHex(Value: Int8): string;
function IntToHex(Value: UInt8): string;
function IntToHex(Value: Int16): string;
function IntToHex(Value: UInt16): string;
function IntToHex(Value: Int32): string;
function IntToHex(Value: UInt32): string;
function IntToHex(Value: Int64): string;
function IntToHex(Value: UInt64): string;
function IntToHex(Value: Integer; Digits: Integer): string;
function IntToHex(Value: Int64; Digits: Integer): string;
function IntToHex(Value: UInt64; Digits: Integer): string;
Exemple :
procedure TForm1.Button1Click(Sender: TObject);
var r, g, b : integer;
begin
Entier7 := 147483647;
 Label1.Caption := 'Hexadécimale : '+IntToHex(123, 2);
 // Label1.Caption = Hexadécimale : 7B
 Label1.Caption := 'Hexadécimale : '+IntToHex(123, 8);
 // Label1.Caption = Hexadécimale : 0000007B
 Label1.Caption := 'Hexadécimale : '+IntToHex(int8(123));
 // Label1.Caption = Hexadécimale : 7B
 Label1.Caption := 'Hexadécimale : '+IntToHex(int16(123));
 // Label1.Caption = Hexadécimale : 007B
 Label1.Caption := 'Hexadécimale : '+IntToHex(int32(123));
 // Label1.Caption = Hexadécimale : 0000007B
  
 // Convertit une couleur (R,G,B) en couleur HTML (Ici couleur violet)
 r := 255;
 g := 0;
 b := 255;
 Label1.Caption := 'Couleur HTML : #'+IntToHex(r, 2)+IntToHex(g, 2)+IntToHex(b, 2);
 // Label1.Caption = Couleur HTML : #FF00FF
 
end;

IntToStr :

IntToStr : Convertir un entier en une chaîne de caractères.

function IntToStr(Value: Integer): string;
function IntToStr(Value: Int64): string;
Exemple :
procedure TForm1.Button1Click(Sender: TObject);
var PetitEntier : Integer;
GrosEntier :  Int64;
begin
PetitEntier := 1364736472;
GrosEntier := 4276372036854775807;;
 Label1.Caption := 'Petit entier : '+IntToStr(PetitEntier);
 // Label1.Caption = Petit entier : 1364736472
 Label1.Caption := 'Gros entier : '+IntToStr(GrosEntier);
 // Label1.Caption = Gros entier : 4276372036854775807
  Label1.Caption := 'Addition : '+IntToStr(126 + 47);
 // Label1.Caption = Addition : 173
end;

Str :

Str : Convertit une expression de type entier ou réel en chaîne de caractères.

procedure Str(const X [: Width [:Decimals]]; var S: String);
Exemple :
procedure TForm1.Button1Click(Sender: TObject);
 var
   UnNombre : real;
   MaChaine : string;
begin
UnNombre := 123456.789;
 Str(UnNombre: 16: 6, MaChaine); // Longueur de chaîne prédéfinie : 16. Décimales : 6.
 Label1.Caption := MaChaine;
 // Label1.Caption = 123456.789000
end;

1818 visites depuis le 06/01/2023