Home  |  Delphi .net Home  |  System.Int16  |  ToString Method
ToString  
Method  
Converts the current Int16 value to a string
Int16 Class
System NameSpace
CF1.  Function ToString ( ) : String ;
CF2.  Function ToString ( Formatting : String; ) : String ;
CF3.  Function ToString ( Formatting:StringFormatting : String; FormatProvider : IFormatProvider; ) : String ;
CF4.  Function ToString ( FormatProvider : IFormatProvider; ) : String;
CF : Methods with this mark are Compact Framework Compatible
Description
The ToString method concerts the current Int16 value into a string representation.
 
By default, with no parameters, it is equivalent to ToString('G') - 'General' formatting is carried.
 
The Formatting string (or array of strings) provides the desired formatting of the Int16 value.
 
The formatting value has the following syntax :
 
FormattingChar[Precision]
 
The formatting character can be one of the following values :
 
C or c Currency such as ?1,234.56
D or d Integer formatting such as 123456
E or e Exponential (Scientific) such as 1.23456E+003
F or f Fixed Point such as 1234.56
G or g General : type dependent
N or n Number : -d,ddd,ddd.ddd... format
P or p Percent such as 69.5%
R or r Round-trip - guarantees reverse formatting
X or x Hex such as E06FC (using X) or e06fc (using x)

 
The FormatProvider parameter determines the parsing rules and is beyond the scope of this article.
Notes
ToString without parameters is a method inherited from the base System.Object (TObject) class. It provides a ready method for classes to convert their content into a string format.
Microsoft MSDN Links
System
System.Int16
 
 
Examples of the various formattings
// Full Unit code.
// -------------------------------------------------------------
// Create a new WinForm application, double click the form to
// create an OnLoad event, and then replace the WinForm unit
// with this text.
 
unit WinForm;
 
interface
 
uses
  System.Drawing, System.Collections, System.ComponentModel,
System.Windows.Forms, System.Data;
 
type
  TWinForm = class(System.Windows.Forms.Form)
  \{REGION 'Designer Managed Code'\} // Note that REGION and ENREGION should be prefixed by a dollar sign
  strict private
    ///
    /// Required designer variable.
    ///

    Components: System.ComponentModel.Container;
    ///
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    ///

    procedure InitializeComponent;
    procedure TWinForm_Load(sender: System.Object; e: System.EventArgs);
  {ENDREGION}
  strict protected
    ///
    /// Clean up any resources being used.
    ///

    procedure Dispose(Disposing: Boolean); override;
  private
    { Private Declarations }
  public
    constructor Create;
  end;
 
  [assembly: RuntimeRequiredAttribute(TypeOf(TWinForm))]
 
implementation
 
\{REGION 'Windows Form Designer generated code'\}
///
/// Required method for Designer support -- do not modify
/// the contents of this method with the code editor.
///

 
 
procedure TWinForm.InitializeComponent;
begin
  //
  // TWinForm
  //
  Self.AutoScaleBaseSize := System.Drawing.Size.Create(5, 13);
  Self.ClientSize := System.Drawing.Size.Create(292, 266);
  Self.Name := 'TWinForm';
  Self.Text := 'WinForm';
  Include(Self.Load, Self.TWinForm_Load);
end;
{ENDREGION}
 
procedure TWinForm.Dispose(Disposing: Boolean);
begin
  if Disposing then
  begin
    if Components <> nil then
      Components.Dispose();
  end;
  inherited Dispose(Disposing);
end;
 
constructor TWinForm.Create;
begin
  inherited Create;
  //
  // Required for Windows Form Designer support
  //
  InitializeComponent;
  //
  // TODO: Add any constructor code after InitializeComponent call
  //
end;
 
procedure TWinForm.TWinForm_Load(sender: System.Object; e: System.EventArgs);
program Project1;
{$APPTYPE CONSOLE}

var
  valueA : Int16;

begin
  valueA := 1234;

  Console.WriteLine('    Default    = {0}', valueA.ToString);
  Console.WriteLine('C : Currency   = {0}', valueA.ToString('C'));
  Console.WriteLine('D : Integer    = {0}', valueA.ToString('D'));
  Console.WriteLine('E : Scientific = {0}', valueA.ToString('E'));
  Console.WriteLine('F : Fixed      = {0}', valueA.ToString('F'));
  Console.WriteLine('G : General    = {0}', valueA.ToString('G'));
  Console.WriteLine('N : Numeric    = {0}', valueA.ToString('N'));
  Console.WriteLine('P : Percent    = {0}', valueA.ToString('P'));
  Console.WriteLine('X : Hex        = {0}', valueA.ToString('X'));

  Console.ReadLine;
end.
 
end.
Hide full unit code
      Default    = 1234
  C : Currency   = ?1,234.00
  D : Integer    = 1234
  E : Scientific = 1.234000E+003
  F : Fixed      = 1234.00
  G : General    = 1234
  N : Numeric    = 1,234.00
  P : Percent    = 123,400.00 %
  X : Hex        = 4D2
Using the optional precision parameter
program Project1;
{$APPTYPE CONSOLE}

var
  valueA : Int16;

begin
  valueA := 1234;

  Console.WriteLine('    Default    = {0}', valueA.ToString);
  Console.WriteLine('C : Currency   = {0}', valueA.ToString('C6'));
  Console.WriteLine('D : Integer    = {0}', valueA.ToString('D6'));
  Console.WriteLine('E : Scientific = {0}', valueA.ToString('E6'));
  Console.WriteLine('F : Fixed      = {0}', valueA.ToString('F6'));
  Console.WriteLine('G : General    = {0}', valueA.ToString('G6'));
  Console.WriteLine('N : Numeric    = {0}', valueA.ToString('N6'));
  Console.WriteLine('P : Percent    = {0}', valueA.ToString('P6'));
  Console.WriteLine('X : Hex        = {0}', valueA.ToString('X6'));

  Console.ReadLine;
end.
Show full unit code
      Default    = 1234
  C : Currency   = ?1,234.000000
  D : Integer    = 001234
  E : Scientific = 1.234000E+003
  F : Fixed      = 1234.000000
  G : General    = 1234
  N : Numeric    = 1,234.000000
  P : Percent    = 123,400.000000 %
  X : Hex        = 0004D2
 
 
Delphi Programming © Neil Moffatt All rights reserved.  |  Contact the author