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

Vous trouverez sur cette page des exemples. Comment convertir en nombre une chaîne qui représente un entier (décimal ou hexadécimal).? Comment convertir une chaîne de caractères en valeur à virgule flottante? Comment convertir une chaînes de caractères en valeurs d'un TDateTime ou TDate?

CONVERSION DE CHAÎNES EN NOMBRES :
  • StringToWideChar : Convertir une chaîne ANSI vers UNICODE.
  • StrToCurr : Convertir une chaîne représentant une valeur en virgule flottante en une valeur Currency.
  • StrToDate : Convertir une chaîne spécifiant une date (J/M/A) en TDateTime.
  • StrToDateTime : Convertir une chaîne spécifiant une date et heure (J/M/A HH:MM:SS) en TDateTime.
  • StrToFloat : Convertir chaîne en valeur à virgule flottante.
  • StrToInt : Convertir en nombre une chaîne qui représente un entier (décimal ou hexadécimal).
  • StrToInt64 : Convertir une chaîne qui représente un entier (décimal ou hexadécimal) en Int64.
  • StrToInt64Def : Convertir en nombre une chaîne qui représente un entier (décimal ou hexadécimal).
  • StrToIntDef : Convertir une chaîne qui représente un entier (décimal ou hexadécimal) en Integer.
  • StrToTime : Convertir une chaîne en valeur TDateTime.
  • Val : Convertir en nombre une chaîne qui représente un entier (décimal ou hexadécimal).
StringToWideChar :

StringToWideChar : Convertir une chaîne ANSI vers UNICODE et stocke le résultat dans une mémoire tampon spécifiée.

function StringToWideChar(const Source: UnicodeString; Dest: PWideChar; DestSize: Integer): PWideChar;
Exemple :
procedure TForm1.Button1Click(Sender: TObject);
var
  wideChars   : array[0..20] of WideChar;
  laChaine    : String;

begin
  LaChaine := 'Bonjour à vous';

  // Copie la chaîne dans un tableau au format WideChar
  StringToWideChar(LaChaine, wideChars, 15);

  // Affiche la copie
  Label1.Caption := WideCharToString(wideChars);
  // Label1.Caption = Bonjour à vous
end;

StrToCurr :

StrToCurr : Convertir une chaîne représentant une valeur en virgule flottante en une valeur Currency.

function StrToCurr(const S: string): Currency;
function StrToCurr(const S: string; const AFormatSettings: TFormatSettings): Currency;
Exemple :
procedure TForm1.Button1Click(Sender: TObject);
var
  ChaineValue : string;
  currValue   : Currency;
 begin
  // chaîne source contenant une représentation numérique
  ChaineValue := '12345,6789';
 
  // Convertit la virgule en point
  currValue  := StrToCurr(ChaineValue);
  // currValue = 12345.6789
 
 Label1.Caption := CurrToStr(currValue);
  // Label1.Caption = 12345,6789
end;

StrToDate :

StrToDate : Convertir une chaîne spécifiant une date (J/M/A) en TDateTime.

function StrToDate(const S: string): TDateTime;
function StrToDate(const S: string; const AFormatSettings: TFormatSettings): TDateTime;
Exemple :
procedure TForm1.Button1Click(Sender: TObject);
var
  LaDate : TDateTime;
 begin
  LaDate := StrToDate('27/06/22');
  Label1.Caption := '27/06/22 : '+DateToStr(LaDate);
  // Label1.Caption = 27/06/22 : 27/06/2022
  LaDate := StrToDate('27/06/2022');
  Label1.Caption := '27/06/2022 : '+DateToStr(LaDate);
  // Label1.Caption = 27/06/2022 : 27/06/2022
end;

StrToDateTime :

StrToDateTime : Convertir une chaîne spécifiant une date et heure (J/M/A HH:MM:SS) en TDateTime.

function StrToDateTime(const S: string): TDateTime;
function StrToDateTime(const S: string; const AFormatSettings: TFormatSettings): TDateTime;
Exemple :
procedure TForm1.Button1Click(Sender: TObject);
var
  LaDateetHeure : TDateTime;
 begin
  LaDateetHeure := StrToDate('27/06/22 12');
  Label1.Caption := '27/06/22 12 : '+DateToStr(LaDateetHeure);
  // Label1.Caption = 27/06/22 12 : 27/06/2022 12:00:00
  LaDateetHeure := StrToDate('27/06/2022');
  Label1.Caption := '27/06/2022 13:24:56 : '+DateTimeToStr(LaDate);
  // Label1.Caption = 27/06/2022 13:24:56 : 27/06/2022 13:24:56
end;

StrToFloat :

StrToFloat : Convertir chaîne en valeur à virgule flottante.

function StrToFloat(const S: string): Extended;
function StrToFloat(const S: string; const AFormatSettings: TFormatSettings): Extended;
Exemple :
procedure TForm1.Button1Click(Sender: TObject);
var
  ChaineValue : string;
  floatValue   : Extended;
 begin
  // chaîne source contenant une représentation numérique
  ChaineValue := '12345,6789';
 
  // Convertit la virgule en point
  floatValue  := StrToFloat(ChaineValue);
  // floatValue = 12345.6789
 
 Label1.Caption := FloatToStr(floatValue);
  // Label1.Caption = 12345,6789
end;

StrToInt :

StrToInt : Convertir en nombre une chaîne qui représente un entier (décimal ou hexadécimal).

function StrToInt(const S: string): Integer;
Exemple :
procedure TForm1.Button1Click(Sender: TObject);
var
  A, B, C, D, E, F : Integer;
begin
  A := 32;
  B := StrToInt('100');
  C := StrToInt('  -12');
  D := StrToInt('$1E');
  E := StrToInt('-0x1E');
  F := A + B + C + D + E;

  ShowMessage('A : '+IntToStr(A)); // Renvoie A : 32
  ShowMessage('B : '+IntToStr(B)); // Renvoie B : 100
  ShowMessage('C : '+IntToStr(C)); // Renvoie C : -12
  ShowMessage('D : '+IntToStr(D)); // Renvoie D : 30
  ShowMessage('E : '+IntToStr(E)); // Renvoie E : -30
  ShowMessage('F : '+IntToStr(F)); // Renvoie F : 120
end;

StrToInt64 :

StrToInt64 : Convertir une chaîne qui représente un entier (décimal ou hexadécimal) en Int64.

function StrToInt64(const S: string): Int64;
Exemple :
procedure TForm1.Button1Click(Sender: TObject);
var
  A, B, C, D, E, F : Int64;
begin
  A := 32;
  B := StrToInt64('100');
  C := StrToInt64('  -12');
  D := StrToInt64('$1E');
  E := StrToInt64('-0x1E');
  F := A + B + C + D + E;

  ShowMessage('A : '+IntToStr(A)); // Renvoie A : 32
  ShowMessage('B : '+IntToStr(B)); // Renvoie B : 100
  ShowMessage('C : '+IntToStr(C)); // Renvoie C : -12
  ShowMessage('D : '+IntToStr(D)); // Renvoie D : 30
  ShowMessage('E : '+IntToStr(E)); // Renvoie E : -30
  ShowMessage('F : '+IntToStr(F)); // Renvoie F : 120
end;

StrToInt64Def :

StrToInt64Def : Convertit en nombre une chaîne qui représente un entier (décimal ou hexadécimal).

function StrToInt64Def(const S: string; const Default: Int64): Int64;
Exemple :
procedure TForm1.Button1Click(Sender: TObject);
var
  A, B, C, D, E, F : Int64;
begin
  A := 32;
  B := StrToInt64Def('100', 0);
  C := StrToInt64Def('  -12', 0);
  D := StrToInt64Def('$1E', 0);
  E := StrToInt64Def('-0x1E', 0);
  F := A + B + C + D + E;

  ShowMessage('A : '+IntToStr(A)); // Renvoie A : 32
  ShowMessage('B : '+IntToStr(B)); // Renvoie B : 100
  ShowMessage('C : '+IntToStr(C)); // Renvoie C : -12
  ShowMessage('D : '+IntToStr(D)); // Renvoie D : 30
  ShowMessage('E : '+IntToStr(E)); // Renvoie E : -30
  ShowMessage('F : '+IntToStr(F)); // Renvoie F : 120
end;

StrToIntDef :

StrToIntDef : Convertir une chaîne qui représente un entier (décimal ou hexadécimal) en Integer.

function StrToIntDef(const S: string; Default: Integer): Integer;
Exemple :
procedure TForm1.Button1Click(Sender: TObject);
var
  A, B, C, D, E, F : Integer;
begin
  A := 32;
  B := StrToIntDef('100', 0);
  C := StrToIntDef('  -12', 0);
  D := StrToIntDef('$1E', 0);
  E := StrToIntDef('-0x1E', 0);
  F := A + B + C + D + E;

  ShowMessage('A : '+IntToStr(A)); // Renvoie A : 32
  ShowMessage('B : '+IntToStr(B)); // Renvoie B : 100
  ShowMessage('C : '+IntToStr(C)); // Renvoie C : -12
  ShowMessage('D : '+IntToStr(D)); // Renvoie D : 30
  ShowMessage('E : '+IntToStr(E)); // Renvoie E : -30
  ShowMessage('F : '+IntToStr(F)); // Renvoie F : 120
end;

StrToTime :

StrToTime : Convertir une chaîne qui spécifie une heure. Si S ne contient pas une heure valide, StrToTime déclenche une exception EConvertError.

function StrToTime(const S: string): TDateTime;
function StrToTime(const S: string; const AFormatSettings: TFormatSettings): TDateTime;
Exemple :
procedure TForm1.Button1Click(Sender: TObject);
 var
  Heure : TDateTime;
begin
  Heure := StrToTime('5PM');
  Label1.Caption := '5PM : '+TimeToStr(Heure);
  //Label1.Caption = 5PM : 17:00:00
  Heure := StrToTime('5AM');
  Label1.Caption := '5AM : '+TimeToStr(Heure);
  //Label1.Caption = 5AM : 05:00:00
  Heure := StrToTime('17');
  Label1.Caption := '17 : '+TimeToStr(Heure);
  //Label1.Caption = 17 : 17:00:00
  Heure := StrToTime('14:25');
  Label1.Caption := '14:25 : '+TimeToStr(Heure);
  //Label1.Caption = 14:25 : 14:25:00
  Heure := StrToTime('14:25:45');
  Label1.Caption := '14:25:45 : '+TimeToStr(Heure);
  //Label1.Caption = 14:25:45 : 14:25:45
end;

Val :

Val : Convertir en nombre une chaîne qui représente un entier (décimal ou hexadécimal). Si la chaîne n'est pas valide, l'indice du caractère erroné est stocké dans Code ; sinon, Code est mis à zéro.

procedure Val(S: String; var V; var Code: Integer);
Exemple :
procedure TForm1.Button1Click(Sender: TObject);
var
  Entier, Code: Integer;
  NombreVirgule : Extended;
  MaChaine : string;
begin
MaChaine := '12345.6789';
  // Convertir la chaîne en Extended
  Val(MaChaine, NombreVirgule, Code);
  { Affiche le résultat }
   Label1.Caption := FloatToStr(NombreVirgule); // 12345,6789
   Label2.Caption := 'Code = '+IntToStr(Code); // Code = 0

  MaChaine := '12345,6789';
  // Convertir la chaîne en Extended
  Val(MaChaine, NombreVirgule, Code);
  { Affiche le résultat }
   Label1.Caption := FloatToStr(NombreVirgule); // 12345
   Label2.Caption := 'Code = '+IntToStr(Code); // Code = 6

  MaChaine := '123456789';
  // Convertir la chaîne en Integer
  Val(MaChaine, Entier, Code);
  { Affiche le résultat }
   Label1.Caption := IntToStr(Entier); // 123456789
   Label2.Caption := 'Code = '+IntToStr(Code); // Code = 0

   MaChaine := 'abcd';
  // Convertir la chaîne en Integer
  Val(MaChaine, Entier, Code);
  { Affiche le résultat }
   Label1.Caption := IntToStr(Entier); // 0
   Label2.Caption := 'Code = '+IntToStr(Code); // Code = 1
 end;

1273 visites depuis le 06/01/2023