Float Type Helpers (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example is a simple Delphi console application that shows how to use the new helper methods and fields for float types.

Code

uses
  System.SysUtils;

var

  mySingle: array [1 .. 6] of Single;
  aFormatSettings: TFormatSettings;
  i: Integer;

begin
  // Initialize an instance of the TFormatSettings class with the U.S. English settings.
  // This is used by some methods in the float helper types.
  aFormatSettings := TFormatSettings.Create('en-US');
  // Initialize the random number generator
  System.Randomize;
  for i := 1 to 6 do
  begin
    // Initialize the values in the mySingle array with the Byte Sign, Mantissa and Exponent representing random numbers.
    mySingle[i].BuildUp(System.Random(2).ToBoolean, System.Random(20),
      System.Random(10));
    // Perform conversion to string.
    write(Format('No. %d float type number is:', [i]));
    writeln(mySingle[i].ToString(aFormatSettings));
    // Separately write the numbers' components.
    writeln(Format('Mantissa: %d ', [mySingle[i].Mantissa]));
    writeln(Format('Exponent: %d ', [mySingle[i].Exponent]));
    writeln;

  end;
  readln;

end.

Uses

See Also