| Description |
The ToString method concerts the current Single 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.Single
|
|
|
| Examples of the various formattings |
program Project1;
{$APPTYPE CONSOLE}
var
value : Single;
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 | program Project1;
{$APPTYPE CONSOLE}
var
value : Single;
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.
| | Show 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 %
|
| |
|
|
|