String Convertion functions

Top 

Example code : ToBoolean

procedure TForm1.W3Button16Click(Sender: TObject);

var

  str1, str2 : string;

  n1,n2   : float;

  same : Boolean;

  small, large, i : Integer;

  amount1, amount2, amount3 : Extended;

begin

  str1 := 'true';

  str2 := '123.456E+002';

 

  same := str1.ToBoolean;

  WriteLn(same);

 

  same := str2.ToBoolean;

  WriteLn(same);

 

  same := '1'.ToBoolean;

  WriteLn(same);

 

  same := '0'.ToBoolean;

  WriteLn(same);

end;

true

false

true

false

------

 

mytoggle_plus1JS output

 

Example code : ToFloat and ToFloatDef

procedure TForm1.W3Button16Click(Sender: TObject);

var

  A, B, C, D, E, F : Float;

  str1 : string;

begin

str1 := '124.58';

  A := str1.ToFloat();

  B := '$FG'.ToFloatDef(1.89);    // '100' string converted to 100 integer

  C := '$FFFFF'.ToFloatDef(2.77);  // Leading blanks are ignored

  D := '9999.898'.ToFloatDef(3.88);    // Hexadecimal values start with a '$'

  E := '-0.8945'.ToFloatDef(4.99);  // ... or with a '0x'

  F := A + B + C + D + E;     // Lets add up all these integers

 

  WriteLn('A : '+FloatToStr(A));

  WriteLn('B : '+FloatToStr(B));

  WriteLn('C : '+FloatToStr(C));

  WriteLn('D : '+FloatToStr(D));

  WriteLn('E : '+FloatToStr(E));

  WriteLn('F : '+FloatToStr(F));

end;

A : 124.58

B : 1.89

C : 2.77

D : 9999.898

E : -0.8945

F : 10128.243499999999

 

mytoggle_plus1JS output

 

Example code : ToInteger and ToIntegerDef

procedure TForm1.W3Button16Click(Sender: TObject);

var

  A, B, C, D, E, F : Integer;

  str1, str2, str3, str4 : string;

begin

  str1 := '100';

  str2 := '  -12';

  str3 := '$FE' ;

  str4 := '-$FE';

  A := str1.ToInteger;

  //str1.ToIntegerDef(str1);

  B := str1.ToIntegerDef(0);    // '100' string converted to 100 integer

  C := str2.ToIntegerDef(0);  // Leading blanks are ignored

  D := str3.ToIntegerDef(66);    // Hexadecimal values start with a '$'

  E := str4.ToIntegerDef(55);  // ... or with a '0x'

  F := A + B + C + D + E;     // Lets add up all these integers

 

  WriteLn('a: '+ IntToStr( '100 '.ToIntegerDef(123)) ); // Trailing blanks are not supported

  WriteLn('b: '+ IntToStr( '$FE'.ToIntegerDef(-1)) );

  WriteLn('c: '+ IntToStr( '-$FE'.ToIntegerDef(-1)) );

  WriteLn('d: '+ IntToStr( '0xFE'.ToIntegerDef(-2)) );

  WriteLn('e: '+ IntToStr( '-0xFE'.ToIntegerDef(-2)) );

 

  WriteLn('A : '+IntToStr(A));

  WriteLn('B : '+IntToStr(B));

  WriteLn('C : '+IntToStr(C));

  WriteLn('D : '+IntToStr(D));

  WriteLn('E : '+IntToStr(E));

  WriteLn('F : '+IntToStr(F));

end;

a: 123 

b: 254 

c: -254

d: 254

e: -254

 

A : 100

B : 100

C : -12

D : 66

E : 55

F : 309 

------------

mytoggle_plus1JS output