Single.ToString 方法
定义
将此实例的数值转换为其等效的字符串表示形式。Converts the numeric value of this instance to its equivalent string representation.
重载
ToString(String, IFormatProvider) |
使用指定的格式和区域性特定格式信息,将此实例的数值转换为它的等效字符串表示形式。Converts the numeric value of this instance to its equivalent string representation using the specified format and culture-specific format information. |
ToString(String) |
使用指定的格式,将此实例的数值转换为它的等效字符串表示形式。Converts the numeric value of this instance to its equivalent string representation, using the specified format. |
ToString(IFormatProvider) |
使用指定的区域性特定格式信息,将此实例的数值转换为它的等效字符串表示形式。Converts the numeric value of this instance to its equivalent string representation using the specified culture-specific format information. |
ToString() |
将此实例的数值转换为其等效的字符串表示形式。Converts the numeric value of this instance to its equivalent string representation. |
ToString(String, IFormatProvider)
使用指定的格式和区域性特定格式信息,将此实例的数值转换为它的等效字符串表示形式。Converts the numeric value of this instance to its equivalent string representation using the specified format and culture-specific format information.
public:
virtual System::String ^ ToString(System::String ^ format, IFormatProvider ^ provider);
public string ToString (string format, IFormatProvider provider);
public string ToString (string? format, IFormatProvider? provider);
override this.ToString : string * IFormatProvider -> string
Public Function ToString (format As String, provider As IFormatProvider) As String
参数
- format
- String
一个数值格式字符串。A numeric format string.
- provider
- IFormatProvider
一个提供区域性特定的格式设置信息的对象。An object that supplies culture-specific formatting information.
返回
此实例的值的字符串表示形式,由 format
和 provider
指定。The string representation of the value of this instance as specified by format
and provider
.
实现
示例
下面的示例 Single 使用多个不同区域性的每个受支持的标准数值格式说明符显示一个值。The following example displays a Single value using each of the supported standard numeric format specifiers for several different cultures.
float value = 16325.62901F;
string specifier;
CultureInfo culture;
// Use standard numeric format specifiers.
specifier = "G";
culture = CultureInfo.CreateSpecificCulture("eu-ES");
Console.WriteLine(value.ToString(specifier, culture));
// Displays: 16325,62901
Console.WriteLine(value.ToString(specifier, CultureInfo.InvariantCulture));
// Displays: 16325.62901
specifier = "C";
culture = CultureInfo.CreateSpecificCulture("en-US");
Console.WriteLine(value.ToString(specifier, culture));
// Displays: $16,325.63
culture = CultureInfo.CreateSpecificCulture("en-GB");
Console.WriteLine(value.ToString(specifier, culture));
// Displays: £16,325.63
specifier = "E04";
culture = CultureInfo.CreateSpecificCulture("sv-SE");
Console.WriteLine(value.ToString(specifier, culture));
// Displays: 1,6326E+004
culture = CultureInfo.CreateSpecificCulture("en-NZ");
Console.WriteLine(value.ToString(specifier, culture));
// Displays: 1.6326E+004
specifier = "F";
culture = CultureInfo.CreateSpecificCulture("fr-FR");
Console.WriteLine(value.ToString(specifier, culture));
// Displays: 16325,63
culture = CultureInfo.CreateSpecificCulture("en-CA");
Console.WriteLine(value.ToString(specifier, culture));
// Displays: 16325.63
specifier = "N";
culture = CultureInfo.CreateSpecificCulture("es-ES");
Console.WriteLine(value.ToString(specifier, culture));
// Displays: 16.325,63
culture = CultureInfo.CreateSpecificCulture("fr-CA");
Console.WriteLine(value.ToString(specifier, culture));
// Displays: 16 325,63
specifier = "P";
culture = CultureInfo.InvariantCulture;
Console.WriteLine((value/10000).ToString(specifier, culture));
// Displays: 163.26 %
culture = CultureInfo.CreateSpecificCulture("ar-EG");
Console.WriteLine((value/10000).ToString(specifier, culture));
// Displays: 163.256 %
Dim value As Single = 16325.62901
Dim specifier As String
Dim culture As CultureInfo
' Use standard numeric format specifiers.
specifier = "G"
culture = CultureInfo.CreateSpecificCulture("eu-ES")
Console.WriteLine(value.ToString(specifier, culture))
' Displays: 16325,62901
Console.WriteLine(value.ToString(specifier, CultureInfo.InvariantCulture))
' Displays: 16325.62901
specifier = "C"
culture = CultureInfo.CreateSpecificCulture("en-US")
Console.WriteLine(value.ToString(specifier, culture))
' Displays: $16,325.63
culture = CultureInfo.CreateSpecificCulture("en-GB")
Console.WriteLine(value.ToString(specifier, culture))
' Displays: £16,325.63
specifier = "E04"
culture = CultureInfo.CreateSpecificCulture("sv-SE")
Console.WriteLine(value.ToString(specifier, culture))
' Displays: 1,6326E+004
culture = CultureInfo.CreateSpecificCulture("en-NZ")
Console.WriteLine(value.ToString(specifier, culture))
' Displays: 1.6326E+004
specifier = "F"
culture = CultureInfo.CreateSpecificCulture("fr-FR")
Console.WriteLine(value.ToString(specifier, culture))
' Displays: 16325,63
culture = CultureInfo.CreateSpecificCulture("en-CA")
Console.WriteLine(value.ToString(specifier, culture))
' Displays: 16325.63
specifier = "N"
culture = CultureInfo.CreateSpecificCulture("es-ES")
Console.WriteLine(value.ToString(specifier, culture))
' Displays: 16.325,63
culture = CultureInfo.CreateSpecificCulture("fr-CA")
Console.WriteLine(value.ToString(specifier, culture))
' Displays: 16 325,63
specifier = "P"
culture = CultureInfo.InvariantCulture
Console.WriteLine((value/10000).ToString(specifier, culture))
' Displays: 163.26 %
culture = CultureInfo.CreateSpecificCulture("ar-EG")
Console.WriteLine((value/10000).ToString(specifier, culture))
' Displays: 163.256 %
注解
方法使用指定 ToString(String, IFormatProvider) Single 的区域性的指定格式设置值的格式。The ToString(String, IFormatProvider) method formats a Single value in a specified format of a specified culture. 如果要使用默认格式或区域性设置,请使用方法的其他重载 ToString ,如下所示:If you want to use default format or culture settings, use the other overloads of the ToString method, as follows:
使用格式To use format | 对于区域性For culture | 使用重载Use the overload |
---|---|---|
默认 ( "G" ) 格式Default ("G") format | 默认 (当前) 区域性Default (current) culture | ToString() |
默认 ( "G" ) 格式Default ("G") format | 特定区域性A specific culture | ToString(IFormatProvider) |
特定格式A specific format | 默认 (当前) 区域性Default (current) culture | ToString(String) |
返回值可以是 PositiveInfinitySymbol 、、 NegativeInfinitySymbol NaNSymbol 或当前实例的值的字符串表示形式,由指定 format
。The return value can be PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or the string representation of the value of the current instance, as specified by format
.
format
参数可以是任何有效的标准数值格式说明符(D 和 X 除外)以及自定义数字格式说明符的任意组合。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. 如果 format
为 null
或空字符串,则此实例的返回值的格式为常规数值格式说明符 ( "G" ) 。If format
is null
or an empty string, the return value for this instance is formatted with the general numeric format specifier ("G").
.NET Framework 提供了广泛的格式设置支持,以下格式设置主题更详细地介绍了这些支持:The .NET Framework 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.
provider
参数是一个 IFormatProvider 实现,其 GetFormat 方法返回 NumberFormatInfo 对象。The provider
parameter is an IFormatProvider implementation whose GetFormat method returns a NumberFormatInfo object. 通常, provider
是 CultureInfo 对象或 NumberFormatInfo 对象。Typically, provider
is a CultureInfo object or a NumberFormatInfo object. provider
参数提供用于设置格式的区域性特定信息。The provider
parameter supplies culture-specific information used in formatting. 如果 provider
为 null
,则返回值的格式为 NumberFormatInfo 当前区域性的对象。If provider
is null
, the return value is formatted with the NumberFormatInfo object for the current culture.
默认情况下,返回值仅包含7位数的精度,尽管它在内部维护最多9位数字。By default, the return value only contains 7 digits of precision although a maximum of 9 digits is maintained internally. 如果此实例的值的位数大于7,则 ToString 返回 PositiveInfinitySymbol 或 NegativeInfinitySymbol 而不是预期的数字。If the value of this instance has greater than 7 digits, ToString returns PositiveInfinitySymbol or NegativeInfinitySymbol instead of the expected number. 如果需要更高的精度,请指定 format
with "G9" 格式规范(始终返回9位精度)或 "R" (如果数字可以用该精度表示,则返回7位数)。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.
另请参阅
- Parse(String)
- String
- 设置 .NET 中类型的格式Formatting Types in .NET
- 如何:用前导零填充数字How to: Pad a Number with Leading Zeros
适用于
ToString(String)
使用指定的格式,将此实例的数值转换为它的等效字符串表示形式。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
参数
- format
- String
一个数值格式字符串。A numeric format string.
返回
此实例的值的字符串表示形式,由 format
指定。The string representation of the value of this instance as specified by format
.
例外
format
无效。format
is invalid.
示例
下面的示例定义一个数值,并使用 "C" 标准数字格式字符串将其格式设置为货币值,并使用 "N" 标准数字格式字符串将其设置为三个小数位数的数字值。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. 使用 en-us 区域性的约定对结果字符串进行格式设置。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
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
下面的示例 Single 使用每个受支持的标准数值格式说明符和两个自定义数字格式字符串显示多个值。The following example displays several Single values using each of the supported standard numeric format specifiers together with two custom numeric format strings. 其中一个自定义格式字符串说明了如何 Single 用前导零填充值。One of those custom format strings illustrates how to pad a Single value with leading zeros. 将数值转换为字符串时,此示例使用 en-us 区域性的格式设置约定。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
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
注解
ToString(String)方法 Single 使用当前区域性的约定来设置指定格式的值的格式。The ToString(String) method formats a Single value in a specified format by using the conventions of the current culture. 如果要使用默认 ( "G" 或常规) 格式或指定不同的区域性,请使用方法的其他重载,如下所示 ToString :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:
使用格式To use format | 对于区域性For culture | 使用重载Use the overload |
---|---|---|
默认 ( "G" ) 格式Default ("G") format | 默认 (当前) 区域性Default (current) culture | ToString() |
默认 ( "G" ) 格式Default ("G") format | 特定区域性A specific culture | ToString(IFormatProvider) |
特定格式A specific format | 特定区域性A specific culture | ToString(String, IFormatProvider) |
返回值可以是 PositiveInfinitySymbol 、、 NegativeInfinitySymbol NaNSymbol 或当前实例的值的字符串表示形式,由指定 format
。The return value can be PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or the string representation of the value of the current instance, as specified by format
.
format
参数可以是任何有效的标准数值格式说明符(D 和 X 除外)以及自定义数字格式说明符的任意组合。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. 如果 format 为 null
或空字符串,则返回值的格式为常规数值格式说明符 ( "G" ) 。If format is null
or an empty string, the return value is formatted with the general numeric format specifier ("G").
.NET Framework 提供了广泛的格式设置支持,以下格式设置主题更详细地介绍了这些支持:The .NET Framework 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.
默认情况下,返回值仅包含7位数的精度,尽管它在内部维护最多9位数字。By default, the return value only contains 7 digits of precision although a maximum of 9 digits is maintained internally. 如果此实例的值的位数大于7,则 ToString(String) 返回 PositiveInfinitySymbol 或 NegativeInfinitySymbol 而不是预期的数字。If the value of this instance has greater than 7 digits, ToString(String) returns PositiveInfinitySymbol or NegativeInfinitySymbol instead of the expected number. 如果需要更高的精度,请指定 format
with "G9" 格式规范(始终返回9位精度)或 "R" (如果数字可以用该精度表示,则返回7位数)。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.
另请参阅
- Parse(String)
- String
- 设置 .NET 中类型的格式Formatting Types in .NET
- 如何:用前导零填充数字How to: Pad a Number with Leading Zeros
适用于
ToString(IFormatProvider)
使用指定的区域性特定格式信息,将此实例的数值转换为它的等效字符串表示形式。Converts the numeric value of this instance to its equivalent string representation using the specified culture-specific format information.
public:
virtual System::String ^ ToString(IFormatProvider ^ provider);
public:
System::String ^ ToString(IFormatProvider ^ provider);
public string ToString (IFormatProvider provider);
public string ToString (IFormatProvider? provider);
override this.ToString : IFormatProvider -> string
Public Function ToString (provider As IFormatProvider) As String
参数
- provider
- IFormatProvider
一个提供区域性特定的格式设置信息的对象。An object that supplies culture-specific formatting information.
返回
此实例的值的字符串表示形式,由 provider
指定。The string representation of the value of this instance as specified by provider
.
实现
示例
下面的示例 Single 使用 CultureInfo 表示多个不同区域性的对象显示两个值的字符串表示形式。The following example displays the string representation of two Single values using CultureInfo objects that represent several different cultures.
float value;
value = -16325.62015F;
// Display value using the invariant culture.
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture));
// Display value using the en-GB culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB")));
// Display value using the de-DE culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")));
value = 16034.125E21F;
// Display value using the invariant culture.
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture));
// Display value using the en-GB culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB")));
// Display value using the de-DE culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")));
// This example displays the following output to the console:
// -16325.62015
// -16325.62015
// -16325,62015
// 1.6034125E+25
// 1.6034125E+25
// 1,6034125E+25
Dim value As Single
value = -16325.62015
' Display value using the invariant culture.
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture))
' Display value using the en-GB culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB")))
' Display value using the de-DE culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")))
value = 16034.125E21
' Display value using the invariant culture.
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture))
' Display value using the en-GB culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB")))
' Display value using the de-DE culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")))
' This example displays the following output to the console:
' -16325.62015
' -16325.62015
' -16325,62015
' 1.6034125E+25
' 1.6034125E+25
' 1,6034125E+25
注解
ToString(IFormatProvider)方法将 Single 默认 ( "G" 中的值的格式设置为指定区域性的常规) 格式。The ToString(IFormatProvider) method formats a Single value in the default ("G", or general) format of a specified culture. 如果要指定其他格式或当前区域性,请使用方法的其他重载,如下所示 ToString :If you want to specify a different format or the current culture, use the other overloads of the ToString method, as follows:
使用格式To use format | 对于区域性For culture | 使用重载Use the overload |
---|---|---|
默认 ( "G" ) 格式Default ("G") format | 默认 (当前) 区域性Default (current) culture | ToString() |
特定格式A specific format | 默认 (当前) 区域性Default (current) culture | ToString(String) |
特定格式A specific format | 特定区域性A specific culture | ToString(String, IFormatProvider) |
返回值可以是 PositiveInfinitySymbol 、 NegativeInfinitySymbol 、 NaNSymbol 或格式的字符串:The return value can be PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or a string of the form:
[sign] 整数位数 [. [小数位数]] [e [sign] 指数-数字][sign]integral-digits[.[fractional-digits]][e[sign]exponential-digits]
可选元素括在方括号中, ( [和] ) 。Optional elements are framed in square brackets ([ and ]). 包含术语 "数字" 的元素包含一系列从0到9之间的数字字符。Elements containing the term "digits" consist of a series of numeric characters ranging from 0 to 9. 下表列出了每个元素。The following table lists each element.
元素Element | 说明Description |
---|---|
Signsign | 负号或正号符号。A negative sign or positive sign symbol. |
整数位数integral-digits | 指定数字的整数部分的一系列数字。A series of digits specifying the integral part of the number. 如果有小数位数,则可以不存在整数位数。Integral-digits can be absent if there are fractional-digits. |
'.''.' | 区域性特定的小数点符号。A culture-specific decimal point symbol. |
小数位数fractional-digits | 指定数字小数部分的一系列数字。A series of digits specifying the fractional part of the number. |
电邮'e' | 小写字符 "e",表示 (科学记数法) 表示法。A lowercase character 'e', indicating exponential (scientific) notation. |
指数位数exponential-digits | 指定指数的一系列数字。A series of digits specifying an exponent. |
返回值的一些示例包括 "100"、"-123456789"、"123.45 e + 6"、"500"、"3.1416"、"600"、"-0.123" 和 "-无限大"。Some examples of the return value are "100", "-123,456,789", "123.45e+6", "500", "3.1416", "600", "-0.123", and "-Infinity".
.NET Framework 提供了广泛的格式设置支持,以下格式设置主题更详细地介绍了这些支持:The .NET Framework 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.
provider
参数是一个 IFormatProvider 实现,其 GetFormat 方法返回 NumberFormatInfo 对象。The provider
parameter is an IFormatProvider implementation whose GetFormat method returns a NumberFormatInfo object. 通常, provider
是 CultureInfo 对象或 NumberFormatInfo 对象。Typically, provider
is a CultureInfo object or a NumberFormatInfo object. provider
参数提供用于设置格式的区域性特定信息。The provider
parameter supplies culture-specific information used in formatting. 如果 provider
为 null
,则使用当前区域性的数据设置返回值的格式 NumberFormatInfo 。If provider
is null
, the return value is formatted using the NumberFormatInfo data for the current culture.
若要 Single 使用指定的区域性和特定格式字符串将值转换为其字符串表示形式,请调用 Single.ToString(String, IFormatProvider) 方法。To convert a Single value to its string representation using a specified culture and a specific format string, call the Single.ToString(String, IFormatProvider) method.
另请参阅
适用于
ToString()
将此实例的数值转换为其等效的字符串表示形式。Converts the numeric value of this instance to its equivalent string representation.
public:
override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String
返回
此实例的值的字符串表示形式。The string representation of the value of this instance.
示例
下面的示例使用默认 Single.ToString 方法显示若干值的字符串表示形式 Single 。The following example uses the default Single.ToString method to display the string representations of a number of Single values.
float number;
number = 1.6E20F;
// Displays 1.6E+20.
Console.WriteLine(number.ToString());
number = 1.6E2F;
// Displays 160.
Console.WriteLine(number.ToString());
number = -3.541F;
// Displays -3.541.
Console.WriteLine(number.ToString());
number = -1502345222199E-07F;
// Displays -150234.5222199.
Console.WriteLine(number.ToString());
number = -15023452221990199574E-09F;
// Displays -15023452221.9902.
Console.WriteLine(number.ToString());
number = .60344F;
// Displays 0.60344.
Console.WriteLine(number.ToString());
number = .000000001F;
// Displays 1E-09.
Console.WriteLine(number.ToString());
Dim number As Single
number = 1.6E20
' Displays 1.6E+20.
Console.WriteLine(number.ToString())
number = 1.6E2
' Displays 160.
Console.WriteLine(number.ToString())
number = -3.541
' Displays -3.541.
Console.WriteLine(number.ToString())
number = -1502345222199E-07
' Displays -150234.5222199.
Console.WriteLine(number.ToString())
number = -15023452221990199574E-09
' Displays -15023452221.9902.
Console.WriteLine(number.ToString())
number = .60344
' Displays 0.60344.
Console.WriteLine(number.ToString())
number = .000000001
' Displays 1E-09.
Console.WriteLine(number.ToString())
下面的代码示例演示如何将 Parse(String) 方法与方法一起使用 ToString() 。The following code example illustrates the use of the Parse(String) method along with the ToString() method.
bool done = false;
String^ inp;
do
{
Console::Write( "Enter a real number: " );
inp = Console::ReadLine();
try
{
s = Single::Parse( inp );
Console::WriteLine( "You entered {0}.", s );
done = true;
}
catch ( FormatException^ )
{
Console::WriteLine( "You did not enter a number." );
}
catch ( Exception^ e )
{
Console::WriteLine( "An exception occurred while parsing your response: {0}", e );
}
}
while ( !done );
bool done = false;
string inp;
do
{
Console.Write("Enter a real number: ");
inp = Console.ReadLine();
try
{
s = Single.Parse(inp);
Console.WriteLine("You entered {0}.", s.ToString());
done = true;
}
catch (FormatException)
{
Console.WriteLine("You did not enter a number.");
}
catch (Exception e)
{
Console.WriteLine("An exception occurred while parsing your response: {0}", e.ToString());
}
} while (!done);
Dim Done As Boolean = False
Dim Inp As String
Do
Console.Write("Enter a real number: ")
Inp = Console.ReadLine()
Try
S = Single.Parse(Inp)
Console.WriteLine("You entered " + S.ToString() + ".")
Done = True
Catch E As FormatException
Console.WriteLine("You did not enter a number.")
Catch E As Exception
Console.WriteLine("An exception occurred while parsing your response: " + E.ToString())
End Try
Loop While Not Done
注解
ToString()方法使用 Single 当前区域性的默认 ( "G" 或常规) 格式设置值的格式。The ToString() method formats a Single value in the default ("G", or general) format of the current culture. 如果要指定其他格式或区域性,请使用方法的其他重载 ToString ,如下所示:If you want to specify a different format or culture, use the other overloads of the ToString method, as follows:
使用格式To use format | 对于区域性For culture | 使用重载Use the overload |
---|---|---|
默认 ( "G" ) 格式Default ("G") format | 特定区域性A specific culture | ToString(IFormatProvider) |
特定格式A specific format | 默认 (当前) 区域性Default (current) culture | ToString(String) |
特定格式A specific format | 特定区域性A specific culture | ToString(String, IFormatProvider) |
返回值可以是 PositiveInfinitySymbol 、 NegativeInfinitySymbol 、 NaNSymbol 或格式的字符串:The return value can be PositiveInfinitySymbol, NegativeInfinitySymbol, NaNSymbol, or a string of the form:
[sign] 整数位数 [. [小数位数]] [e [sign] 指数-数字][sign]integral-digits[.[fractional-digits]][e[sign]exponential-digits]
可选元素括在方括号中, ( [和] ) 。Optional elements are framed in square brackets ([ and ]). 包含术语 "数字" 的元素包含一系列从0到9之间的数字字符。Elements that contain the term "digits" consist of a series of numeric characters ranging from 0 to 9. 下表列出了每个元素:The following table lists each element:
元素Element | 说明Description |
---|---|
signsign | 负号或正号符号。A negative sign or positive sign symbol. |
整数位数integral-digits | 指定数字的整数部分的一系列数字。A series of digits specifying the integral part of the number. 如果有小数位数,则可以不存在整数位数。Integral-digits can be absent if there are fractional-digits. |
'.''.' | 区域性特定的小数点符号。A culture-specific decimal point symbol. |
小数位数fractional-digits | 指定数字小数部分的一系列数字。A series of digits specifying the fractional part of the number. |
电邮'e' | 小写字符 "e",表示 (科学记数法) 表示法。A lowercase character 'e', indicating exponential (scientific) notation. |
指数位数exponential-digits | 指定指数的一系列数字。A series of digits specifying an exponent. |
返回值的一些示例包括 "100"、"-123456789"、"123.45 e + 6"、"500"、"3.1416"、"600"、"-0.123" 和 "-无限大"。Some examples of the return value are "100", "-123,456,789", "123.45e+6", "500", "3.1416", "600", "-0.123", and "-Infinity".
.NET Framework 提供了广泛的格式设置支持,以下格式设置主题更详细地介绍了这些支持:The .NET Framework 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.