Mega Code Archive

 
Categories / Delphi / Functions
 

Str a function more useful than a procedure

Title: Str: a function more useful than a procedure. Question: I desired to have a str function instead of a Str procedure so.. I built it. By overloading the functions, Delphi is capable of converting anything to a string. Besides, you can extend it to any variable type. Answer: Here is the code: interface function str_(const x : double) : string; overload; function str_(const x : longint) : string; overload; function str_(const x : longword) : string; overload; function str_(const x : boolean) : string; overload; implementation function str_(const x: double ) : string; overload; var aux : string; begin str(x:8:2,aux); str_:=aux; end; function str_(const x: longint ) : string; overload; var aux : string; begin str(x:8,aux); str_:=aux; end; function str_(const x: longword) : string; overload; var aux : string; begin str(x:8,aux); str_:=aux; end; function str_(const x: boolean ) : string; overload; begin if x then str_:='true' else str_:='false'; end;