| Description |
The ToString method concerts the current Double 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 Double 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.Double
|
|
|
| Examples of the various formattings |
program Project1;
{$APPTYPE CONSOLE}
var
value : Double;
begin
value := 123.456;
Console.WriteLine(' Default = {0}', value.ToString);
Console.WriteLine('C : Currency = {0}', value.ToString('C'));
Console.WriteLine('E : Scientific = {0}', value.ToString('E'));
Console.WriteLine('F : Fixed = {0}', value.ToString('F'));
Console.WriteLine('G : General = {0}', value.ToString('G'));
Console.WriteLine('N : Numeric = {0}', value.ToString('N'));
Console.WriteLine('P : Percent = {0}', value.ToString('P'));
Console.ReadLine;
end.
| | Show full unit code | Default = 123.456
C : Currency = ?123.46
E : Scientific = 1.234560E+002
F : Fixed = 123.46
G : General = 123.456
N : Numeric = 123.46
P : Percent = 12,345.60 %
| | | Using the optional precision parameter | // 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
value : Double;
begin
value := 123.456;
Console.WriteLine(' Default = {0}', value.ToString);
Console.WriteLine('C : Currency = {0}', value.ToString('C6'));
Console.WriteLine('E : Scientific = {0}', value.ToString('E6'));
Console.WriteLine('F : Fixed = {0}', value.ToString('F6'));
Console.WriteLine('G : General = {0}', value.ToString('G6'));
Console.WriteLine('N : Numeric = {0}', value.ToString('N6'));
Console.WriteLine('P : Percent = {0}', value.ToString('P6'));
Console.ReadLine;
end. end. | | Hide full unit code | Default = 123.456
C : Currency = ?123.456000
E : Scientific = 1.234560E+002
F : Fixed = 123.456000
G : General = 123.456
N : Numeric = 123.456000
P : Percent = 12,345.600000 %
|
| |
|
|
|