Converts the numeric value of this instance to its equivalent string representation, using the specified format.
public:
System::String ^ ToString(System::String ^ format);
public string ToString(string format);
public string ToString(string? format);
override this.ToString : string -> string
Public Function ToString (format As String) As String
Parameters
A numeric format string.
ReturnsThe string representation of the value of this instance as specified by format
.
The following example defines a numeric value and formats it as a currency value by using the "C" standard numeric format string and as a numeric value to three decimal places by using the "N" standard numeric format string. The result strings are formatted by using the conventions of the en-US culture. For more information on numeric format strings, see Standard Numeric Format Strings and Custom Numeric Format Strings.
using System;
public class Example
{
public static void Main()
{
Double number = 1764.3789;
// Format as a currency value.
Console.WriteLine(number.ToString("C"));
// Format as a numeric value with 3 decimal places.
Console.WriteLine(number.ToString("N3"));
}
}
// The example displays the following output:
// $1,764.38
// 1,764.379
let number = 1764.3789
// Format as a currency value.
printfn $"""{number.ToString "C"}"""
// Format as a numeric value with 3 decimal places.
printfn $"""{number.ToString "N3"}"""
// The example displays the following output:
// $1,764.38
// 1,764.379
Module Example
Public Sub Main()
Dim number As Single = 1764.3789
' Format as a currency value.
Console.WriteLine(number.ToString("C"))
' Format as a numeric value with 3 decimal places.
Console.WriteLine(number.ToString("N3"))
End Sub
End Module
' The example displays the following output:
' $1,764.38
' 1,764.379
The following example displays several Single values using each of the supported standard numeric format specifiers together with two custom numeric format strings. One of those custom format strings illustrates how to pad a Single value with leading zeros. In converting the numeric values to strings, the example uses the formatting conventions of the en-US culture.
float[] numbers= { 1054.32179F, -195489100.8377F, 1.0437E21F,
-1.0573e-05F };
string[] specifiers = { "C", "E", "e", "F", "G", "N", "P",
"R", "#,000.000", "0.###E-000",
"000,000,000,000.00###" };
foreach (float number in numbers)
{
Console.WriteLine("Formatting of {0}:", number);
foreach (string specifier in specifiers)
Console.WriteLine(" {0,5}: {1}",
specifier, number.ToString(specifier));
Console.WriteLine();
}
// The example displays the following output to the console:
// Formatting of 1054.32179:
// C: $1,054.32
// E: 1.054322E+003
// e: 1.054322e+003
// F: 1054.32
// G: 1054.32179
// N: 1,054.32
// P: 105,432.18 %
// R: 1054.32179
// #,000.000: 1,054.322
// 0.###E-000: 1.054E003
// 000,000,000,000.00###: 000,000,001,054.322
//
// Formatting of -195489100.8377:
// C: ($195,489,100.84)
// E: -1.954891E+008
// e: -1.954891e+008
// F: -195489100.84
// G: -195489100.8377
// N: -195,489,100.84
// P: -19,548,910,083.77 %
// R: -195489100.8377
// #,000.000: -195,489,100.838
// 0.###E-000: -1.955E008
// 000,000,000,000.00###: -000,195,489,100.00
//
// Formatting of 1.0437E+21:
// C: $1,043,700,000,000,000,000,000.00
// E: 1.043700E+021
// e: 1.043700e+021
// F: 1043700000000000000000.00
// G: 1.0437E+21
// N: 1,043,700,000,000,000,000,000.00
// P: 104,370,000,000,000,000,000,000.00 %
// R: 1.0437E+21
// #,000.000: 1,043,700,000,000,000,000,000.000
// 0.###E-000: 1.044E021
// 000,000,000,000.00###: 1,043,700,000,000,000,000,000.00
//
// Formatting of -1.0573E-05:
// C: $0.00
// E: -1.057300E-005
// e: -1.057300e-005
// F: 0.00
// G: -1.0573E-05
// N: 0.00
// P: 0.00 %
// R: -1.0573E-05
// #,000.000: 000.000
// 0.###E-000: -1.057E-005
// 000,000,000,000.00###: -000,000,000,000.00001
let numbers =
[| 1054.32179F; -195489100.8377F; 1.0437E21F; -1.0573e-05F |]
let specifiers =
[| "C"; "E"; "e"; "F"; "G"; "N"; "P"
"R"; "#,000.000"; "0.###E-000"
"000,000,000,000.00###" |]
for number in numbers do
printfn $"Formatting of {number}:"
for specifier in specifiers do
printfn $" {specifier,5}: {number.ToString specifier}"
printfn ""
// The example displays the following output to the console:
// Formatting of 1054.32179:
// C: $1,054.32
// E: 1.054322E+003
// e: 1.054322e+003
// F: 1054.32
// G: 1054.32179
// N: 1,054.32
// P: 105,432.18 %
// R: 1054.32179
// #,000.000: 1,054.322
// 0.###E-000: 1.054E003
// 000,000,000,000.00###: 000,000,001,054.322
//
// Formatting of -195489100.8377:
// C: ($195,489,100.84)
// E: -1.954891E+008
// e: -1.954891e+008
// F: -195489100.84
// G: -195489100.8377
// N: -195,489,100.84
// P: -19,548,910,083.77 %
// R: -195489100.8377
// #,000.000: -195,489,100.838
// 0.###E-000: -1.955E008
// 000,000,000,000.00###: -000,195,489,100.00
//
// Formatting of 1.0437E+21:
// C: $1,043,700,000,000,000,000,000.00
// E: 1.043700E+021
// e: 1.043700e+021
// F: 1043700000000000000000.00
// G: 1.0437E+21
// N: 1,043,700,000,000,000,000,000.00
// P: 104,370,000,000,000,000,000,000.00 %
// R: 1.0437E+21
// #,000.000: 1,043,700,000,000,000,000,000.000
// 0.###E-000: 1.044E021
// 000,000,000,000.00###: 1,043,700,000,000,000,000,000.00
//
// Formatting of -1.0573E-05:
// C: $0.00
// E: -1.057300E-005
// e: -1.057300e-005
// F: 0.00
// G: -1.0573E-05
// N: 0.00
// P: 0.00 %
// R: -1.0573E-05
// #,000.000: 000.000
// 0.###E-000: -1.057E-005
// 000,000,000,000.00###: -000,000,000,000.00001
Dim numbers() As Single = {1054.32179, -195489100.8377, 1.0437E21, _
-1.0573e-05}
Dim specifiers() As String = { "C", "E", "e", "F", "G", "N", "P", _
"R", "#,000.000", "0.###E-000", _
"000,000,000,000.00###"}
For Each number As Single In numbers
Console.WriteLine("Formatting of {0}:", number)
For Each specifier As String In specifiers
Console.WriteLine(" {0,5}: {1}", _
specifier, number.ToString(specifier))
Next
Console.WriteLine()
Next
' The example displays the following output to the console:
' Formatting of 1054.32179:
' C: $1,054.32
' E: 1.054322E+003
' e: 1.054322e+003
' F: 1054.32
' G: 1054.32179
' N: 1,054.32
' P: 105,432.18 %
' R: 1054.32179
' #,000.000: 1,054.322
' 0.###E-000: 1.054E003
' 000,000,000,000.00###: 000,000,001,054.322
'
' Formatting of -195489100.8377:
' C: ($195,489,100.84)
' E: -1.954891E+008
' e: -1.954891e+008
' F: -195489100.84
' G: -195489100.8377
' N: -195,489,100.84
' P: -19,548,910,083.77 %
' R: -195489100.8377
' #,000.000: -195,489,100.838
' 0.###E-000: -1.955E008
' 000,000,000,000.00###: -000,195,489,100.00
'
' Formatting of 1.0437E+21:
' C: $1,043,700,000,000,000,000,000.00
' E: 1.043700E+021
' e: 1.043700e+021
' F: 1043700000000000000000.00
' G: 1.0437E+21
' N: 1,043,700,000,000,000,000,000.00
' P: 104,370,000,000,000,000,000,000.00 %
' R: 1.0437E+21
' #,000.000: 1,043,700,000,000,000,000,000.000
' 0.###E-000: 1.044E021
' 000,000,000,000.00###: 1,043,700,000,000,000,000,000.00
'
' Formatting of -1.0573E-05:
' C: $0.00
' E: -1.057300E-005
' e: -1.057300e-005
' F: 0.00
' G: -1.0573E-05
' N: 0.00
' P: 0.00 %
' R: -1.0573E-05
' #,000.000: 000.000
' 0.###E-000: -1.057E-005
' 000,000,000,000.00###: -000,000,000,000.00001
Remarks
The ToString(String) method formats a Single value in a specified format by using the conventions of the current culture. If you want to use the default ("G", or general) format or specify a different culture, use the other overloads of the ToString method, as follows:
The return value can be PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or the string representation of the value of the current instance, as specified by format
.
The format
parameter can be any valid standard numeric format specifier except for D and X, as well as any combination of custom numeric format specifiers. If format is null
or an empty string, the return value is formatted with the general numeric format specifier ("G").
.NET provides extensive formatting support, which is described in greater detail in the following formatting topics:
For more information about numeric format specifiers, see Standard Numeric Format Strings and Custom Numeric Format Strings.
For more information about formatting, see Formatting Types.
By default, the return value only contains 7 digits of precision although a maximum of 9 digits is maintained internally. If the value of this instance has greater than 7 digits, ToString(String) returns PositiveInfinitySymbol or NegativeInfinitySymbol instead of the expected number. If you require more precision, specify format
with the "G9" format specification, which always returns 9 digits of precision, or "R", which returns 7 digits if the number can be represented with that precision or 9 digits if the number can only be represented with maximum precision.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4