21 agosto 2007

Convertir cualquier tipo de variable a String (y II)

Vamos a seguir viendo funciones para la conversión 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, precisión y dígitos según los parámetros Format, Precision y Digits respectivamente. Por ejemplo:

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

function Format( const Format: string; const Args: array of const ): string; overload;

Esta función es similar al comando printf del lenguaje C. Su misión es dar múltiples formatos a una misma cadena de texto según sus argumentos. Con algunos ejemplos se verá más 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 = Decimal (Integer)
e = Científico
f = Punto fijo
g = General
m = Moneda
n = Número (Real)
p = Puntero (La dirección de memoria)
s = String
u = Decimal sin signo
x = Hexadecimal

Dentro de una misma cadena de formato se pueden introducir distintos parámetros:

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 tendríamos 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 según el formato determinado por el parámetro Format. Por ejemplo:

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

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

Esta función 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 función FormatCurr. Por ejemplo:

FormatFloat( '#####', 1234.5678 ) devuelve 1235
FormatFloat( '00000', 1234.5678 ) devuelve 01235
FormatFloat( '########', 1234.5678 ) devuelve 1235
FormatFloat( '00000000', 1234.5678 ) devuelve 00001235
FormatFloat( '0.####', 1234.5678 ) devuelve 1234,5678
FormatFloat( '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 número de dígitos a través del parámetro 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. Además da la posibilidad de especificar el ancho en dígitos del número y sus decimales. Por ejemplo:

var
sTexto: String;
begin
Str( 1234, sTexto ); // sTexto = '1234'
Str( 1234:10, sTexto ); // sTexto = ' 1234'
Str( 1234.5678:10:2, sTexto ); // sTexto = ' 1234.57'
Str( 1234.5678:10:4, sTexto ); // sTexto = ' 1234.5678'
end;

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.

No hay comentarios:

Publicidad