Está en la página 1de 3

Convertir cualquier tipo de variable a

String
(II)
Vamos a seguir viendo funciones para la conversin de cualquier tipo de
variable a String:
function FloatToStr( Value: Extended ): string; overload;
Convierte un valor Extended en String. Por ejemplo:
FloatToStr( 1234.5678 ) devuelve 1234,5678

function FloatToStrF( Value: Extended; Format: TFloatFormat; Precision,


Digits: Integer ): string; overload;
Convierte un valor Extended en String usando el formato, precisin y dgitos
segn los parmetros Format, Precision y Digits respectivamente. Por
ejemplo:
FloatToStrF(
FloatToStrF(
FloatToStrF(
FloatToStrF(
FloatToStrF(
FloatToStrF(
FloatToStrF(
FloatToStrF(
FloatToStrF(
FloatToStrF(

1234.5678,
1234.5678,
1234.5678,
1234.5678,
1234.5678,
1234.5678,
1234.5678,
1234.5678,
1234.5678,
1234.5678,

ffCurrency, 8, 2 ) devuelve 1.234,57


ffCurrency, 8, 4 ) devuelve 1.234,5678
ffGeneral, 8, 2 ) devuelve 1234,5678
ffGeneral, 8, 4 ) devuelve 1234,5678
ffExponent, 8, 2 ) devuelve 1,2345678E+03
ffExponent, 8, 4 ) devuelve 1,2345678E+0003
ffFixed, 8, 2 ) devuelve 1234,57
ffFixed, 8, 4 ) devuelve 1234,5678
ffNumber, 8, 2 ) devuelve 1.234,57
ffNumber, 8, 4 ) devuelve 1.234,5678

function Format( const Format: string; const Args: array of const ): string;
overload;
Esta funcin es similar al comando printf del lenguaje C. Su misin es dar
mltiples formatos a una misma cadena de texto segn sus argumentos. Con
algunos ejemplos se ver ms claro:
Format( '%d', [1234] ) devuelve -1234
Format( '%e', [1234.5678] ) devuelve 1,23456780000000E+003
Format( '%f', [1234.5678] ) devuelve 1234,57
Format( '%g', [1234.5678] ) devuelve 1234,5678
Format( '%n', [1234.5678] ) devuelve 1.234,57
Format( '%m', [1234.5678] ) devuelve 1.234,57
sTexto := 'Prueba';
Format( '%p', [sTexto] ) devuelve 0012FE14
Format( '%s', [sTexto] ) devuelve Prueba
Format( '%u', [1234] ) devuelve 1234
Format( '%x', [1234] ) devuelve 4D2

Cada los siguientes tipos de formato:


d
e
f
g
m
n
p
s
u
x

=
=
=
=
=
=
=
=
=
=

Decimal (Integer)
Cientfico
Punto fijo
General
Moneda
Nmero (Real)
Puntero (La direccin de memoria)
String
Decimal sin signo
Hexadecimal

Dentro de una misma cadena de formato se pueden introducir distintos


parmetros:
var
sSerie: String;
iNumero: Integer;
rImporte: Real;
begin
sSerie := 'A';
iNumero := 5276;
rImporte := 120.35;
ShowMessage( Format( 'Factura n %s-%d -> Importe %f', [sSerie,
iNumero, rImporte] ) );
end;

Al ejecutarlo muestra:
Factura n A-5276 -> Importe 120,35

De otra manera tendramos que hacer:


ShowMessage( 'Factura n ' + sSerie + '-' + IntToStr( iNumero ) + ' ->
Importe ' + FloatToStr( rImporte ) );

function FormatCurr( const Format: string; Value: Currency ): string;


overload;
Convierte un valor Currency a String segn el formato determinado por el
parmetro Format. Por ejemplo:
FormatCurr(
FormatCurr(
FormatCurr(
FormatCurr(
FormatCurr(
FormatCurr(

'#####', 1234.5678 ) devuelve 1235


'00000', 1234.5678 ) devuelve 01235
'########', 1234.5678 ) devuelve 1235
'00000000', 1234.5678 ) devuelve 00001235
'0.####', 1234.5678 ) devuelve 1234,5678
'0.00000', 1234.5678 ) devuelve 1234,56780

function FormatDateTime( const Format: string; DateTime: TDateTime ):


string; overload;
Esta funcin es similar al procedimiento DateTimeToString para dar formato
a un campo TDateTime. Por ejemplo:
var
dt: TDateTime;
begin
dt := StrToDateTime( '20/06/2007 11:27' );
FormatDateTime( 'd/m/y', dt ); // devuelve 20/6/07
FormatDateTime( 'dd/mm/yyyy', dt ); // devuelve 20/06/2007
FormatDateTime( 'dd-mm-yyyy', dt ); // devuelve 20-06-2007
end;

function FormatFloat( const Format: string; Value: Extended ): string;


overload;
Convierte un valor Extended a String utilizar el formato determinado por
Format. Es similar a la funcin FormatCurr. Por ejemplo:
FormatFloat(
FormatFloat(
FormatFloat(
FormatFloat(
FormatFloat(
FormatFloat(

'#####', 1234.5678 ) devuelve 1235


'00000', 1234.5678 ) devuelve 01235
'########', 1234.5678 ) devuelve 1235
'00000000', 1234.5678 ) devuelve 00001235
'0.####', 1234.5678 ) devuelve 1234,5678
'0.00000', 1234.5678 ) devuelve 1234,56780

function IntToHex( Value: Integer; Digits: Integer ): string; overload;


function IntToHex( Value: Int64; Digits: Integer ): string; overload;

Estas dos funciones convierten un valor Integer o Int64 a String en formato


hexadecimal, especificando el nmero de dgitos a travs del parmetro
Digits. Por ejemplo:
IntToHex( 123456, 6 ) devuelve 01E240
IntToHex( 123456, 5 ) devuelve 1E240
IntToHex( 123456, 4 ) devuelve 1E240

function IntToStr( Value: Integer ): string; overload;


function IntToStr( Value: Int64 ): string; overload;
Ambas funciones convierten un valor Integer o Int64 a String en formato
decimal. Por ejemplo:
IntToStr( 123456 ) devuelve 123456

procedure Str( X [: Width [: Decimals ]]; var S );


Este procedimiento da un formato a la cadena de texto S la cual esta en una
variable. Adems da la posibilidad de especificar el ancho en dgitos del
nmero y sus decimales. Por ejemplo:
var
sTexto: String;
begin
Str( 1234, sTexto );
Str( 1234:10, sTexto
Str( 1234.5678:10:2,
Str( 1234.5678:10:4,
end;

// sTexto = '1234'
); // sTexto = ' 1234'
sTexto ); // sTexto = ' 1234.57'
sTexto ); // sTexto = ' 1234.5678'

function WideCharToString( Source: PWideChar ): string;


Convierte una cadena de texto Unicode (16 bits) a String (8 bits). Por
ejemplo:
var
Unicode: array[0..4] of WideChar;
sTexto: String;
begin
Unicode[0] := 'H';
Unicode[1] := 'o';
Unicode[2] := 'l';
Unicode[3] := 'a';
Unicode[4] := #0; // Terminamos la cadena
sTexto := WideCharToString( Unicode );
end;

Pruebas realizadas en Delphi 7.

También podría gustarte