Single.Parse 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将数字的字符串表示形式转换为它的等效单精度浮点数。
重载
Parse(String) |
将数字的字符串表示形式转换为它的等效单精度浮点数。 |
Parse(ReadOnlySpan<Byte>, IFormatProvider) |
将 UTF-8 字符范围分析为值。 |
Parse(ReadOnlySpan<Char>, IFormatProvider) |
将字符范围解析为值。 |
Parse(String, NumberStyles) |
将具有指定样式的数字的字符串表示形式转换为它的等效单精度浮点数。 |
Parse(String, IFormatProvider) |
将具有指定区域性特定格式的数字的字符串表示形式转换为它的等效单精度浮点数。 |
Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider) |
将 UTF-8 字符范围分析为值。 |
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) |
将字符范围(其中包含指定样式和区域性特定格式的数字的字符串表示形式)转换为它的等效单精度浮点数。 |
Parse(String, NumberStyles, IFormatProvider) |
将具有指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效单精度浮点数。 |
注解
在 .NET Core 3.0 及更高版本中,太大而无法表示的值将舍入到 PositiveInfinity IEEE NegativeInfinity 754 规范的要求。 在以前的版本中(包括.NET Framework)中,分析太大而无法表示的值会导致失败。
Parse(String)
- Source:
- Single.cs
- Source:
- Single.cs
- Source:
- Single.cs
将数字的字符串表示形式转换为它的等效单精度浮点数。
public:
static float Parse(System::String ^ s);
public static float Parse (string s);
static member Parse : string -> single
Public Shared Function Parse (s As String) As Single
参数
- s
- String
包含要转换的数字的字符串。
返回
与 s
中指定的数值或符号等效的单精度浮点数字。
例外
s
为 null
。
s
不表示具有有效格式的数字。
仅 .NET Framework 和 .NET Core 2.2 及更早版本:s
表示小于 Single.MinValue 或大于 Single.MaxValue 的数字。
示例
以下示例使用 Parse(String) 方法将字符串数组转换为等效 Single 值。
using System;
public class Example
{
public static void Main()
{
string[] values = { "100", "(100)", "-123,456,789", "123.45e+6",
"+500", "5e2", "3.1416", "600.", "-.123",
"-Infinity", "-1E-16", Double.MaxValue.ToString(),
Single.MinValue.ToString(), String.Empty };
foreach (string value in values)
{
try {
float number = Single.Parse(value);
Console.WriteLine("{0} -> {1}", value, number);
}
catch (FormatException) {
Console.WriteLine("'{0}' is not in a valid format.", value);
}
catch (OverflowException) {
Console.WriteLine("{0} is outside the range of a Single.", value);
}
}
}
}
// The example displays the following output:
// 100 -> 100
// '(100)' is not in a valid format.
// -123,456,789 -> -1.234568E+08
// 123.45e+6 -> 1.2345E+08
// +500 -> 500
// 5e2 -> 500
// 3.1416 -> 3.1416
// 600. -> 600
// -.123 -> -0.123
// -Infinity -> -Infinity
// -1E-16 -> -1E-16
// 1.79769313486232E+308 is outside the range of a Single.
// -3.402823E+38 -> -3.402823E+38
// '' is not in a valid format.
open System
let values =
[| "100"; "(100)"; "-123,456,789"; "123.45e+6"
"+500"; "5e2"; "3.1416"; "600."; "-.123"
"-Infinity"; "-1E-16"; string Double.MaxValue
string Single.MinValue; String.Empty |]
for value in values do
try
let number = Single.Parse value
printfn $"{value} -> {number}"
with
| :? FormatException ->
printfn $"'{value}' is not in a valid format."
| :? OverflowException ->
printfn $"{value} is outside the range of a Single."
// The example displays the following output:
// 100 -> 100
// '(100)' is not in a valid format.
// -123,456,789 -> -1.234568E+08
// 123.45e+6 -> 1.2345E+08
// +500 -> 500
// 5e2 -> 500
// 3.1416 -> 3.1416
// 600. -> 600
// -.123 -> -0.123
// -Infinity -> -Infinity
// -1E-16 -> -1E-16
// 1.79769313486232E+308 is outside the range of a Single.
// -3.402823E+38 -> -3.402823E+38
// '' is not in a valid format.
Module Example
Public Sub Main()
Dim values() As String = { "100", "(100)", "-123,456,789", "123.45e+6", _
"+500", "5e2", "3.1416", "600.", "-.123", _
"-Infinity", "-1E-16", Double.MaxValue.ToString(), _
Single.MinValue.ToString(), String.Empty }
For Each value As String In values
Try
Dim number As Single = Single.Parse(value)
Console.WriteLine("{0} -> {1}", value, number)
Catch e As FormatException
Console.WriteLine("'{0}' is not in a valid format.", value)
Catch e As OverflowException
Console.WriteLine("{0} is outside the range of a Single.", value)
End Try
Next
End Sub
End Module
' The example displays the following output:
' 100 -> 100
' '(100)' is not in a valid format.
' -123,456,789 -> -1.234568E+08
' 123.45e+6 -> 1.2345E+08
' +500 -> 500
' 5e2 -> 500
' 3.1416 -> 3.1416
' 600. -> 600
' -.123 -> -0.123
' -Infinity -> -Infinity
' -1E-16 -> -1E-16
' 1.79769313486232E+308 is outside the range of a Single.
' -3.402823E+38 -> -3.402823E+38
' '' is not in a valid format.
注解
在 .NET Core 3.0 及更高版本中,太大而无法表示的值将舍入到 PositiveInfinity IEEE NegativeInfinity 754 规范的要求。 在以前的版本中(包括.NET Framework)中,分析太大而无法表示的值会导致失败。
参数 s
可以包含当前区域性的 PositiveInfinitySymbol、 NegativeInfinitySymbol、 NaNSymbol或 格式的字符串:
[ws][sign][integral-digits[,]]integral-digits[.[小数位数]][e[sign]exponential-digits][ws]
方括号 ([ and ]) 中的元素是可选的。 下表对每个元素进行了描述。
元素 | 说明 |
---|---|
ws | 一系列空白字符。 |
sign | 负号符号或正符号。 有效符号字符由 NumberFormatInfo.NegativeSign 当前区域性的 和 NumberFormatInfo.PositiveSign 属性决定。 只能使用前导符号。 |
integral-digits | 一系列数字,范围从 0 到 9,指定数字的整型部分。 整数位数的运行可以按组分隔符进行分区。 例如,在某些区域性中,逗号 (,) 分隔成千组。 如果字符串包含小 数位数 元素,则整数 位 元素可能不存在。 |
, | 区域性特定的千位分隔符。 |
. | 区域性特定的小数点符号。 |
fractional-digits | 一系列数字,范围从 0 到 9,指定数字的小数部分。 |
E | “e”或“E”字符,指示值以指数 (科学) 表示法表示。 |
exponential-digits | 指定指数的一系列数字,范围从 0 到 9。 |
参数s
使用 和 NumberStyles.AllowThousands 标志的组合NumberStyles.Float进行解释。 这意味着允许使用空格和数千个分隔符,但不允许使用货币符号。 若要显式定义 (元素,例如货币符号、千位分隔符和空白) 中可能存在 s
,请使用 Parse(String, NumberStyles) 方法重载。
参数 s
是使用为当前系统区域性初始化的 对象中的 NumberFormatInfo 格式设置信息进行分析的。 有关详细信息,请参阅 CurrentInfo。 若要使用特定区域性的格式设置信息分析字符串,请使用 Parse(String, IFormatProvider) 或 Parse(String, NumberStyles, IFormatProvider) 方法。
通常,如果向 方法传递 Parse 通过调用 ToString 方法创建的字符串,则会返回原始 Single 值。 但是,由于精度损失,这些值可能不相等。
如果 s
已超过数据类型的范围Single,则方法在 .NET Framework 和 .NET Core 2.2 及更早版本上引发 OverflowException 。 在 .NET Core 3.0 及更高版本中,如果 s
小于 Single.MinValue 且 Single.PositiveInfinitys
大于 Single.MaxValue,则返回 Single.NegativeInfinity 。
如果在分析操作期间参数中 s
遇到分隔符,并且适用的货币或数字小数分隔符和组分隔符相同,则分析操作假定分隔符是小数点分隔符,而不是组分隔符。 有关分隔符的详细信息,请参阅 CurrencyDecimalSeparator、 NumberDecimalSeparator、 CurrencyGroupSeparator和 NumberGroupSeparator。
另请参阅
适用于
Parse(ReadOnlySpan<Byte>, IFormatProvider)
- Source:
- Single.cs
- Source:
- Single.cs
将 UTF-8 字符范围分析为值。
public:
static float Parse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider) = IUtf8SpanParsable<float>::Parse;
public static float Parse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);
static member Parse : ReadOnlySpan<byte> * IFormatProvider -> single
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider) As Single
参数
- utf8Text
- ReadOnlySpan<Byte>
要分析的 UTF-8 字符的跨度。
- provider
- IFormatProvider
一个对象,提供有关 utf8Text
的区域性特定格式设置信息。
返回
分析 utf8Text
的结果。
实现
适用于
Parse(ReadOnlySpan<Char>, IFormatProvider)
- Source:
- Single.cs
- Source:
- Single.cs
- Source:
- Single.cs
将字符范围解析为值。
public:
static float Parse(ReadOnlySpan<char> s, IFormatProvider ^ provider) = ISpanParsable<float>::Parse;
public static float Parse (ReadOnlySpan<char> s, IFormatProvider? provider);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> single
Public Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As Single
参数
- s
- ReadOnlySpan<Char>
要分析的字符范围。
- provider
- IFormatProvider
一个对象,提供有关 s
的区域性特定格式设置信息。
返回
分析 s
的结果。
实现
适用于
Parse(String, NumberStyles)
- Source:
- Single.cs
- Source:
- Single.cs
- Source:
- Single.cs
将具有指定样式的数字的字符串表示形式转换为它的等效单精度浮点数。
public:
static float Parse(System::String ^ s, System::Globalization::NumberStyles style);
public static float Parse (string s, System.Globalization.NumberStyles style);
static member Parse : string * System.Globalization.NumberStyles -> single
Public Shared Function Parse (s As String, style As NumberStyles) As Single
参数
- s
- String
包含要转换的数字的字符串。
- style
- NumberStyles
枚举值的按位组合,用于指示可出现在 s
中的样式元素。 一个用来指定的典型值为 Float 与 AllowThousands 的组合。
返回
与在 s
中指定的数值或符号等效的单精度浮点数字。
例外
s
为 null
。
s
不是一个具有有效格式的数字。
仅 .NET Framework 和 .NET Core 2.2 及更早版本:s
表示小于 Single.MinValue 或大于 Single.MaxValue 的数字。
示例
以下示例使用 Parse(String, NumberStyles) 方法分析值的字符串表示形式 Single 。 该示例使用 en-US 区域性的格式设置信息。
using System;
using System.Globalization;
using System.Threading;
public class ParseString
{
public static void Main()
{
// Set current thread culture to en-US.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
string value;
NumberStyles styles;
// Parse a string in exponential notation with only the AllowExponent flag.
value = "-1.063E-02";
styles = NumberStyles.AllowExponent;
ShowNumericValue(value, styles);
// Parse a string in exponential notation
// with the AllowExponent and Number flags.
styles = NumberStyles.AllowExponent | NumberStyles.Number;
ShowNumericValue(value, styles);
// Parse a currency value with leading and trailing white space, and
// white space after the U.S. currency symbol.
value = " $ 6,164.3299 ";
styles = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
ShowNumericValue(value, styles);
// Parse negative value with thousands separator and decimal.
value = "(4,320.64)";
styles = NumberStyles.AllowParentheses | NumberStyles.AllowTrailingSign |
NumberStyles.Float;
ShowNumericValue(value, styles);
styles = NumberStyles.AllowParentheses | NumberStyles.AllowTrailingSign |
NumberStyles.Float | NumberStyles.AllowThousands;
ShowNumericValue(value, styles);
}
private static void ShowNumericValue(string value, NumberStyles styles)
{
Single number;
try
{
number = Single.Parse(value, styles);
Console.WriteLine("Converted '{0}' using {1} to {2}.",
value, styles.ToString(), number);
}
catch (FormatException)
{
Console.WriteLine("Unable to parse '{0}' with styles {1}.",
value, styles.ToString());
}
Console.WriteLine();
}
}
// The example displays the following output to the console:
// Unable to parse '-1.063E-02' with styles AllowExponent.
//
// Converted '-1.063E-02' using AllowTrailingSign, AllowThousands, Float to -0.01063.
//
// Converted ' $ 6,164.3299 ' using Number, AllowCurrencySymbol to 6164.3299.
//
// Unable to parse '(4,320.64)' with styles AllowTrailingSign, AllowParentheses, Float.
//
// Converted '(4,320.64)' using AllowTrailingSign, AllowParentheses, AllowThousands, Float to -4320.64.
open System
open System.Globalization
open System.Threading
let showNumericValue value (styles: NumberStyles) =
try
let number = Single.Parse(value, styles)
printfn $"Converted '{value}' using {styles} to {number}."
with :? FormatException ->
printfn $"Unable to parse '{value}' with styles {styles}."
printfn ""
[<EntryPoint>]
let main _ =
// Set current thread culture to en-US.
Thread.CurrentThread.CurrentCulture <- CultureInfo.CreateSpecificCulture "en-US"
// Parse a string in exponential notation with only the AllowExponent flag.
let value = "-1.063E-02"
let styles = NumberStyles.AllowExponent
showNumericValue value styles
// Parse a string in exponential notation
// with the AllowExponent and Number flags.
let styles = NumberStyles.AllowExponent ||| NumberStyles.Number
showNumericValue value styles
// Parse a currency value with leading and trailing white space, and
// white space after the U.S. currency symbol.
let value = " $ 6,164.3299 "
let styles = NumberStyles.Number ||| NumberStyles.AllowCurrencySymbol
showNumericValue value styles
// Parse negative value with thousands separator and decimal.
let value = "(4,320.64)"
let styles = NumberStyles.AllowParentheses ||| NumberStyles.AllowTrailingSign ||| NumberStyles.Float
showNumericValue value styles
let styles = NumberStyles.AllowParentheses ||| NumberStyles.AllowTrailingSign ||| NumberStyles.Float ||| NumberStyles.AllowThousands
showNumericValue value styles
0
// The example displays the following output to the console:
// Unable to parse '-1.063E-02' with styles AllowExponent.
//
// Converted '-1.063E-02' using AllowTrailingSign, AllowThousands, Float to -0.01063.
//
// Converted ' $ 6,164.3299 ' using Number, AllowCurrencySymbol to 6164.3299.
//
// Unable to parse '(4,320.64)' with styles AllowTrailingSign, AllowParentheses, Float.
//
// Converted '(4,320.64)' using AllowTrailingSign, AllowParentheses, AllowThousands, Float to -4320.64.
Imports System.Globalization
Imports System.Threading
Module ParseStrings
Public Sub Main()
' Set current thread culture to en-US.
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US")
Dim value As String
Dim styles As NumberStyles
' Parse a string in exponential notation with only the AllowExponent flag.
value = "-1.063E-02"
styles = NumberStyles.AllowExponent
ShowNumericValue(value, styles)
' Parse a string in exponential notation
' with the AllowExponent and Number flags.
styles = NumberStyles.AllowExponent Or NumberStyles.Number
ShowNumericValue(value, styles)
' Parse a currency value with leading and trailing white space, and
' white space after the U.S. currency symbol.
value = " $ 6,164.3299 "
styles = NumberStyles.Number Or NumberStyles.AllowCurrencySymbol
ShowNumericValue(value, styles)
' Parse negative value with thousands separator and decimal.
value = "(4,320.64)"
styles = NumberStyles.AllowParentheses Or NumberStyles.AllowTrailingSign _
Or NumberStyles.Float
ShowNumericValue(value, styles)
styles = NumberStyles.AllowParentheses Or NumberStyles.AllowTrailingSign _
Or NumberStyles.Float Or NumberStyles.AllowThousands
ShowNumericValue(value, styles)
End Sub
Private Sub ShowNumericValue(value As String, styles As NumberStyles)
Dim number As Single
Try
number = Single.Parse(value, styles)
Console.WriteLine("Converted '{0}' using {1} to {2}.", _
value, styles.ToString(), number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}' with styles {1}.", _
value, styles.ToString())
End Try
Console.WriteLine()
End Sub
End Module
' The example displays the following output to the console:
' Unable to parse '-1.063E-02' with styles AllowExponent.
'
' Converted '-1.063E-02' using AllowTrailingSign, AllowThousands, Float to -0.01063.
'
' Converted ' $ 6,164.3299 ' using Number, AllowCurrencySymbol to 6164.3299.
'
' Unable to parse '(4,320.64)' with styles AllowTrailingSign, AllowParentheses, Float.
'
' Converted '(4,320.64)' using AllowTrailingSign, AllowParentheses, AllowThousands, Float to -4320.64.
注解
在 .NET Core 3.0 及更高版本中,太大而无法表示的值将舍入到 PositiveInfinity IEEE NegativeInfinity 754 规范的要求。 在以前的版本中(包括.NET Framework)中,分析太大而无法表示的值会导致失败。
参数 style
定义参数中 s
允许的样式元素 (,例如空格、千位分隔符和货币符号) ,以便分析操作成功。 它必须是枚举中的位标志 NumberStyles 的组合。 不支持以下 NumberStyles 成员:
参数s
可以包含当前区域性的 PositiveInfinitySymbol、、 NegativeInfinitySymbolNaNSymbol。 根据 的值 style
,它还可以采用以下形式:
[ws][$][sign][integral-digits[,]]integral-digits[.[小数位数]][E[sign]exponential-digits][ws]
方括号 ([ and ]) 中的元素是可选的。 下表对每个元素进行了描述。
Ws 一系列空格字符。 如果 style
包含标志,则空格可以出现在 开头s
,如果style
包含NumberStyles.AllowTrailingWhite标志,则它可能显示在 s
NumberStyles.AllowLeadingWhite 末尾。
$ 特定于区域性的货币符号。 它在字符串中的位置由 NumberFormatInfo.CurrencyNegativePattern 当前区域性的 和 NumberFormatInfo.CurrencyPositivePattern 属性定义。 如果包含 标志,则当前区域性的货币符号可以显示在 中s
NumberStyles.AllowCurrencySymbol。style
标志 负号符号 ( ) 或正号符号 (+) 。 如果包含 标志,则符号可以出现在 的s
开头,如果style
包含 NumberStyles.AllowTrailingSign 标志,则它可显示在 的末尾s
。NumberStyles.AllowLeadingSignstyle
如果style
包含 标志,NumberStyles.AllowParentheses则可以在 中使用s
括号来指示负值。
integral-digits 一系列数字,范围从 0 到 9,指定数字的整数部分。 如果字符串包含小数位数元素,则整数位元素可能不存在。
,特定于区域性的组分隔符。 如果style
包含 NumberStyles.AllowThousands 标志,则当前区域性的组分隔符可以出现在 中s
.
区域性特定的小数点符号。 如果style
包含 NumberStyles.AllowDecimalPoint 标志,则当前区域性的小数点符号可以出现在 中s
。
fractional-digits 一系列数字,范围从 0 到 9,指定数字的小数部分。 如果style
包含 标志,NumberStyles.AllowDecimalPoint则可以在 中s
显示小数位数。
E“e”或“E”字符,指示值以指数 (科学) 表示法表示。 如果style
包含 标志,则 value
参数可以表示指数表示法中的NumberStyles.AllowExponent数字。
exponential-digits 指定指数的一系列数字,范围为 0 到 9。
注意
分析操作将忽略中 s
任何 (U+0000) 字符的终止 NUL,而不考虑 参数的值 style
。
仅包含数字的字符串 (对应于 NumberStyles.None 样式) 如果位于类型范围内 Single ,则始终会成功分析。 其余 System.Globalization.NumberStyles 成员控制输入字符串中可能存在但不需要存在的元素。 下表指示各个 NumberStyles 标志如何影响 中可能存在 s
的元素。
NumberStyles 值 | 除数字外允许的 s 元素 |
---|---|
None | 仅限 整型数字 元素。 |
AllowDecimalPoint | 小数点 (.) 和 小数位数 元素。 |
AllowExponent | 指示指数表示法的“e”或“E”字符。 此标志本身支持 数字E数字形式的值;需要其他标志才能成功分析包含正数或负号和小数点符号等元素的字符串。 |
AllowLeadingWhite | 开头的 s ws 元素。 |
AllowTrailingWhite | 位于 末尾的 s ws 元素。 |
AllowLeadingSign | 开头的s 符号元素。 |
AllowTrailingSign | 末尾的s 符号元素。 |
AllowParentheses | 用括号括住数值的 符号 元素。 |
AllowThousands | 千位分隔符 (,) 元素。 |
AllowCurrencySymbol | $) 元素 (货币。 |
Currency | 所有元素。 但是, s 不能表示十六进制数或指数表示法中的数字。 |
Float | 位于 开头或末尾的 s ws元素,符号位于 开头,s 小数点 (.) 符号。 参数 s 还可以使用指数表示法。 |
Number |
ws 、 sign 、千分隔符 (,) 和小数点 (.) 元素。 |
Any | 所有元素。 但是, s 不能表示十六进制数。 |
s
例如,“100”、“-123,456,789”、“123.45e+6”、“+500”、“5e2”、“3.1416”、“600.”、“-.123”和“-Infinity”。
参数 s
是使用针对当前系统区域性初始化的 对象中的 NumberFormatInfo 格式设置信息进行分析的。 若要指定其格式设置信息用于分析操作的区域性,请 Parse(String, NumberStyles, IFormatProvider) 调用 重载。
通常,如果向方法传递 Parse 通过调用 ToString 方法创建的字符串,则返回原始 Single 值。 但是,由于精度损失,这些值可能不相等。
如果 s
已超过数据类型的范围Single,则方法在 .NET Framework 和 .NET Core 2.2 及更早版本上引发 OverflowException 。 在 .NET Core 3.0 及更高版本上,如果 s
小于 Single.MinValue ,Single.PositiveInfinity如果 s
大于 Single.MaxValue,则返回 Single.NegativeInfinity 。
如果在分析操作期间在 参数中 s
遇到分隔符,并且适用的货币或数字十进制分隔符和组分隔符相同,则分析操作假定该分隔符是小数分隔符而不是组分隔符。 有关分隔符的详细信息,请参阅 CurrencyDecimalSeparator、 NumberDecimalSeparator、 CurrencyGroupSeparator和 NumberGroupSeparator。
另请参阅
适用于
Parse(String, IFormatProvider)
- Source:
- Single.cs
- Source:
- Single.cs
- Source:
- Single.cs
将具有指定区域性特定格式的数字的字符串表示形式转换为它的等效单精度浮点数。
public:
static float Parse(System::String ^ s, IFormatProvider ^ provider);
public:
static float Parse(System::String ^ s, IFormatProvider ^ provider) = IParsable<float>::Parse;
public static float Parse (string s, IFormatProvider provider);
public static float Parse (string s, IFormatProvider? provider);
static member Parse : string * IFormatProvider -> single
Public Shared Function Parse (s As String, provider As IFormatProvider) As Single
参数
- s
- String
包含要转换的数字的字符串。
- provider
- IFormatProvider
一个对象,提供有关 s
的区域性特定格式设置信息。
返回
与 s
中指定的数值或符号等效的单精度浮点数字。
实现
例外
s
为 null
。
s
不表示具有有效格式的数字。
仅.NET Framework 和 .NET Core 2.2 及更早版本:s
表示小于 Single.MinValue 或大于 Single.MaxValue 的数字。
示例
以下示例是 Web 窗体的按钮单击事件处理程序。 它使用 属性返回的 HttpRequest.UserLanguages 数组来确定用户的区域设置。 然后,它实例化与 CultureInfo 该区域设置对应的 对象。 NumberFormatInfo然后,将属于该CultureInfo对象的 对象传递给 方法,Parse(String, IFormatProvider)以将用户的输入转换为Single值。
protected void OkToSingle_Click(object sender, EventArgs e)
{
string locale;
float number;
CultureInfo culture;
// Return if string is empty
if (String.IsNullOrEmpty(this.inputNumber.Text))
return;
// Get locale of web request to determine possible format of number
if (Request.UserLanguages.Length == 0)
return;
locale = Request.UserLanguages[0];
if (String.IsNullOrEmpty(locale))
return;
// Instantiate CultureInfo object for the user's locale
culture = new CultureInfo(locale);
// Convert user input from a string to a number
try
{
number = Single.Parse(this.inputNumber.Text, culture.NumberFormat);
}
catch (FormatException)
{
return;
}
catch (Exception)
{
return;
}
// Output number to label on web form
this.outputNumber.Text = "Number is " + number.ToString();
}
Protected Sub OkToSingle_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OkToSingle.Click
Dim locale As String
Dim culture As CultureInfo
Dim number As Single
' Return if string is empty
If String.IsNullOrEmpty(Me.inputNumber.Text) Then Exit Sub
' Get locale of web request to determine possible format of number
If Request.UserLanguages.Length = 0 Then Exit Sub
locale = Request.UserLanguages(0)
If String.IsNullOrEmpty(locale) Then Exit Sub
' Instantiate CultureInfo object for the user's locale
culture = New CultureInfo(locale)
' Convert user input from a string to a number
Try
number = Single.Parse(Me.inputNumber.Text, culture.NumberFormat)
Catch ex As FormatException
Exit Sub
Catch ex As OverflowException
Exit Sub
End Try
' Output number to label on web form
Me.outputNumber.Text = "Number is " & number.ToString()
End Sub
注解
在 .NET Core 3.0 及更高版本中,太大而无法表示的值将舍入到 PositiveInfinity IEEE 754 规范要求的 或 NegativeInfinity 。 在以前的版本中(包括 .NET Framework)中,分析太大而无法表示的值会导致失败。
此重载通常用于将可通过多种方式格式化的文本转换为 Single 值。 例如,它可用于将用户在 HTML 文本框中输入的文本转换为数值。
参数s
使用 和 NumberStyles.AllowThousands 标志的组合NumberStyles.Float进行解释。 对于 指定的区域性, s
参数可以包含 NumberFormatInfo.PositiveInfinitySymbol、 NumberFormatInfo.NegativeInfinitySymbol或 NumberFormatInfo.NaNSymbol ,也可以包含 格式的字符串:provider
[ws][sign]integral-digits[.[fractional-digits]][E[sign]exponential-digits][ws]
可选元素在方括号中 ([ 和 ]) 。 包含术语“digits”的元素由一系列介于 0 到 9 的数字字符组成。
元素 | 说明 |
---|---|
ws | 一系列空白字符。 |
sign | 负号符号 ( ) 或正号符号 (+) 。 |
integral-digits | 一系列数字,范围从 0 到 9,指定数字的整数部分。 整位数的运行可以通过分组分隔符进行分区。 例如,在某些区域性中,逗号 (,) 分隔成千上万组。 如果字符串包含小数位数元素,则整数位元素可能不存在。 |
. | 区域性特定的小数点符号。 |
fractional-digits | 一系列数字,范围从 0 到 9,指定数字的小数部分。 |
E | “e”或“E”字符,指示该值以指数 (科学) 表示法表示。 |
exponential-digits | 指定指数的一系列数字,范围为 0 到 9。 |
有关数字格式的详细信息,请参阅 格式设置类型 主题。
provider
参数是一个IFormatProvider实现,其 GetFormat 方法返回一个NumberFormatInfo对象,该对象提供特定于区域性的格式设置信息。
Parse(String, IFormatProvider)调用 方法时,它会调用provider
参数的 GetFormat 方法,并向其传递表示Type类型的 NumberFormatInfo 对象。 然后, GetFormat 方法返回对象, NumberFormatInfo 该对象提供有关参数格式 s
的信息。 有三种方法可以使用 provider
参数向分析操作提供自定义格式设置信息:
可以传递一个 CultureInfo 对象,该对象表示提供格式设置信息的区域性。 其 GetFormat 方法返回对象, NumberFormatInfo 该对象提供该区域性的数字格式设置信息。
可以传递提供数字格式设置信息的实际 NumberFormatInfo 对象。 (它的 实现 GetFormat 只返回自身。)
可以传递实现 的 IFormatProvider自定义对象。 其 GetFormat 方法实例化并返回 NumberFormatInfo 提供格式设置信息的 对象。
如果 provider
为 null
或 NumberFormatInfo 无法获取 ,则使用当前系统区域性的格式设置信息。
如果 s
已超过数据类型的范围Single,则方法在 .NET Framework 和 .NET Core 2.2 及更早版本上引发 OverflowException 。 在 .NET Core 3.0 及更高版本上,如果 s
小于 Single.MinValue ,Single.PositiveInfinity如果 s
大于 Single.MaxValue,则返回 Single.NegativeInfinity 。
如果在分析操作期间在 参数中 s
遇到分隔符,并且适用的货币或数字十进制分隔符和组分隔符相同,则分析操作假定该分隔符是小数分隔符而不是组分隔符。 有关分隔符的详细信息,请参阅 CurrencyDecimalSeparator、 NumberDecimalSeparator、 CurrencyGroupSeparator和 NumberGroupSeparator。
s
例如,“100”、“-123,456,789”、“123.45e+6”、“+500”、“5e2”、“3.1416”、“600.”、“-.123”和“-Infinity”。
另请参阅
适用于
Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)
- Source:
- Single.cs
- Source:
- Single.cs
将 UTF-8 字符范围分析为值。
public static float Parse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider -> single
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), Optional style As NumberStyles = System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, Optional provider As IFormatProvider = Nothing) As Single
参数
- utf8Text
- ReadOnlySpan<Byte>
要分析的 UTF-8 字符范围。
- style
- NumberStyles
可以存在于 中的 utf8Text
数字样式的按位组合。
- provider
- IFormatProvider
一个对象,提供有关 utf8Text
的区域性特定格式设置信息。
返回
分析 utf8Text
的结果。
实现
适用于
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)
- Source:
- Single.cs
- Source:
- Single.cs
- Source:
- Single.cs
将字符范围(其中包含指定样式和区域性特定格式的数字的字符串表示形式)转换为它的等效单精度浮点数。
public static float Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, IFormatProvider? provider = default);
public static float Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, IFormatProvider provider = default);
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> single
Public Shared Function Parse (s As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, Optional provider As IFormatProvider = Nothing) As Single
参数
- s
- ReadOnlySpan<Char>
包含要转换的数字的字符范围。
- style
- NumberStyles
枚举值的按位组合,用于指示可出现在 s
中的样式元素。 一个用来指定的典型值为 Float 与 AllowThousands 的组合。
- provider
- IFormatProvider
一个对象,提供有关 s
的区域性特定格式设置信息。
返回
与在 s
中指定的数值或符号等效的单精度浮点数字。
实现
例外
s
不表示一个数值。
注解
在 .NET Core 3.0 及更高版本中,太大而无法表示的值将舍入到 PositiveInfinity IEEE 754 规范要求的 或 NegativeInfinity 。 在以前的版本中(包括 .NET Framework)中,分析太大而无法表示的值会导致失败。
如果 s
超出数据类型的范围Single,则该方法返回 (如果 s
小于 Single.MinValue ),如果 Single.PositiveInfinity 大于 Single.MaxValue,则s
返回 Single.NegativeInfinity 。
适用于
Parse(String, NumberStyles, IFormatProvider)
- Source:
- Single.cs
- Source:
- Single.cs
- Source:
- Single.cs
将具有指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效单精度浮点数。
public:
static float Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
public:
static float Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider) = System::Numerics::INumberBase<float>::Parse;
public static float Parse (string s, System.Globalization.NumberStyles style, IFormatProvider provider);
public static float Parse (string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> single
Public Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As Single
参数
- s
- String
包含要转换的数字的字符串。
- style
- NumberStyles
枚举值的按位组合,用于指示可出现在 s
中的样式元素。 一个用来指定的典型值为 Float 与 AllowThousands 的组合。
- provider
- IFormatProvider
一个对象,提供有关 s
的区域性特定格式设置信息。
返回
与 s
中指定的数值或符号等效的单精度浮点数字。
实现
例外
s
为 null
。
s
不表示一个数值。
仅 .NET Framework 和 .NET Core 2.2 及更早版本:s
表示小于 Single.MinValue 或大于 Single.MaxValue 的数字。
示例
下面的代码示例使用 Parse(String, NumberStyles, IFormatProvider) 方法分析值的字符串表示形式 Single 。 数组中的每个字符串都是使用 en-US、nl-NL 和自定义区域性的格式设置约定进行分析的。 自定义区域性将其组分隔符定义为下划线 (“_”) ,将其组大小定义为 2。
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
// Define an array of string values.
string[] values = { " 987.654E-2", " 987,654E-2", "(98765,43210)",
"9,876,543.210", "9.876.543,210", "98_76_54_32,19" };
// Create a custom culture based on the invariant culture.
CultureInfo ci = new CultureInfo("");
ci.NumberFormat.NumberGroupSizes = new int[] { 2 };
ci.NumberFormat.NumberGroupSeparator = "_";
// Define an array of format providers.
CultureInfo[] providers = { new CultureInfo("en-US"),
new CultureInfo("nl-NL"), ci };
// Define an array of styles.
NumberStyles[] styles = { NumberStyles.Currency, NumberStyles.Float };
// Iterate the array of format providers.
foreach (CultureInfo provider in providers)
{
Console.WriteLine("Parsing using the {0} culture:",
provider.Name == String.Empty ? "Invariant" : provider.Name);
// Parse each element in the array of string values.
foreach (string value in values)
{
foreach (NumberStyles style in styles)
{
try {
float number = Single.Parse(value, style, provider);
Console.WriteLine(" {0} ({1}) -> {2}",
value, style, number);
}
catch (FormatException) {
Console.WriteLine(" '{0}' is invalid using {1}.", value, style);
}
catch (OverflowException) {
Console.WriteLine(" '{0}' is out of the range of a Single.", value);
}
}
}
Console.WriteLine();
}
}
}
// The example displays the following output:
// Parsing using the en-US culture:
// ' 987.654E-2' is invalid using Currency.
// 987.654E-2 (Float) -> 9.87654
// ' 987,654E-2' is invalid using Currency.
// ' 987,654E-2' is invalid using Float.
// (98765,43210) (Currency) -> -9.876543E+09
// '(98765,43210)' is invalid using Float.
// 9,876,543.210 (Currency) -> 9876543
// '9,876,543.210' is invalid using Float.
// '9.876.543,210' is invalid using Currency.
// '9.876.543,210' is invalid using Float.
// '98_76_54_32,19' is invalid using Currency.
// '98_76_54_32,19' is invalid using Float.
//
// Parsing using the nl-NL culture:
// ' 987.654E-2' is invalid using Currency.
// ' 987.654E-2' is invalid using Float.
// ' 987,654E-2' is invalid using Currency.
// 987,654E-2 (Float) -> 9.87654
// (98765,43210) (Currency) -> -98765.43
// '(98765,43210)' is invalid using Float.
// '9,876,543.210' is invalid using Currency.
// '9,876,543.210' is invalid using Float.
// 9.876.543,210 (Currency) -> 9876543
// '9.876.543,210' is invalid using Float.
// '98_76_54_32,19' is invalid using Currency.
// '98_76_54_32,19' is invalid using Float.
//
// Parsing using the Invariant culture:
// ' 987.654E-2' is invalid using Currency.
// 987.654E-2 (Float) -> 9.87654
// ' 987,654E-2' is invalid using Currency.
// ' 987,654E-2' is invalid using Float.
// (98765,43210) (Currency) -> -9.876543E+09
// '(98765,43210)' is invalid using Float.
// 9,876,543.210 (Currency) -> 9876543
// '9,876,543.210' is invalid using Float.
// '9.876.543,210' is invalid using Currency.
// '9.876.543,210' is invalid using Float.
// 98_76_54_32,19 (Currency) -> 9.876543E+09
// '98_76_54_32,19' is invalid using Float.
open System
open System.Globalization
// Define a list of string values.
let values =
[ " 987.654E-2"; " 987,654E-2"; "(98765,43210)"
"9,876,543.210"; "9.876.543,210"; "98_76_54_32,19" ]
// Create a custom culture based on the invariant culture.
let ci = CultureInfo ""
ci.NumberFormat.NumberGroupSizes <- [| 2 |]
ci.NumberFormat.NumberGroupSeparator <- "_"
// Define a list of format providers.
let providers =
[ CultureInfo "en-US"
CultureInfo "nl-NL"
ci ]
// Define a list of styles.
let styles = [ NumberStyles.Currency; NumberStyles.Float ]
// Iterate the list of format providers.
for provider in providers do
printfn $"""Parsing using the {if provider.Name = String.Empty then "Invariant" else provider.Name} culture:"""
// Parse each element in the array of string values.
for value in values do
for style in styles do
try
let number = Single.Parse(value, style, provider)
printfn $" {value} ({style}) -> {number}"
with
| :? FormatException ->
printfn $" '{value}' is invalid using {style}."
| :? OverflowException ->
printfn $" '{value}' is out of the range of a Single."
printfn ""
// The example displays the following output:
// Parsing using the en-US culture:
// ' 987.654E-2' is invalid using Currency.
// 987.654E-2 (Float) -> 9.87654
// ' 987,654E-2' is invalid using Currency.
// ' 987,654E-2' is invalid using Float.
// (98765,43210) (Currency) -> -9.876543E+09
// '(98765,43210)' is invalid using Float.
// 9,876,543.210 (Currency) -> 9876543
// '9,876,543.210' is invalid using Float.
// '9.876.543,210' is invalid using Currency.
// '9.876.543,210' is invalid using Float.
// '98_76_54_32,19' is invalid using Currency.
// '98_76_54_32,19' is invalid using Float.
//
// Parsing using the nl-NL culture:
// ' 987.654E-2' is invalid using Currency.
// ' 987.654E-2' is invalid using Float.
// ' 987,654E-2' is invalid using Currency.
// 987,654E-2 (Float) -> 9.87654
// (98765,43210) (Currency) -> -98765.43
// '(98765,43210)' is invalid using Float.
// '9,876,543.210' is invalid using Currency.
// '9,876,543.210' is invalid using Float.
// 9.876.543,210 (Currency) -> 9876543
// '9.876.543,210' is invalid using Float.
// '98_76_54_32,19' is invalid using Currency.
// '98_76_54_32,19' is invalid using Float.
//
// Parsing using the Invariant culture:
// ' 987.654E-2' is invalid using Currency.
// 987.654E-2 (Float) -> 9.87654
// ' 987,654E-2' is invalid using Currency.
// ' 987,654E-2' is invalid using Float.
// (98765,43210) (Currency) -> -9.876543E+09
// '(98765,43210)' is invalid using Float.
// 9,876,543.210 (Currency) -> 9876543
// '9,876,543.210' is invalid using Float.
// '9.876.543,210' is invalid using Currency.
// '9.876.543,210' is invalid using Float.
// 98_76_54_32,19 (Currency) -> 9.876543E+09
// '98_76_54_32,19' is invalid using Float.
Imports System.Globalization
Module Example
Public Sub Main()
' Define an array of string values.
Dim values() As String = { " 987.654E-2", " 987,654E-2", _
"(98765,43210)", "9,876,543.210", _
"9.876.543,210", "98_76_54_32,19" }
' Create a custom culture based on the invariant culture.
Dim ci As New CultureInfo("")
ci.NumberFormat.NumberGroupSizes = New Integer() { 2 }
ci.NumberFormat.NumberGroupSeparator = "_"
' Define an array of format providers.
Dim providers() As CultureInfo = { New CultureInfo("en-US"), _
New CultureInfo("nl-NL"), ci }
' Define an array of styles.
Dim styles() As NumberStyles = { NumberStyles.Currency, NumberStyles.Float }
' Iterate the array of format providers.
For Each provider As CultureInfo In providers
Console.WriteLine("Parsing using the {0} culture:", _
If(provider.Name = String.Empty, "Invariant", provider.Name))
' Parse each element in the array of string values.
For Each value As String In values
For Each style As NumberStyles In styles
Try
Dim number As Single = Single.Parse(value, style, provider)
Console.WriteLine(" {0} ({1}) -> {2}", _
value, style, number)
Catch e As FormatException
Console.WriteLine(" '{0}' is invalid using {1}.", value, style)
Catch e As OverflowException
Console.WriteLine(" '{0}' is out of the range of a Single.", value)
End Try
Next
Next
Console.WriteLine()
Next
End Sub
End Module
' The example displays the following output:
' Parsing using the en-US culture:
' ' 987.654E-2' is invalid using Currency.
' 987.654E-2 (Float) -> 9.87654
' ' 987,654E-2' is invalid using Currency.
' ' 987,654E-2' is invalid using Float.
' (98765,43210) (Currency) -> -9.876543E+09
' '(98765,43210)' is invalid using Float.
' 9,876,543.210 (Currency) -> 9876543
' '9,876,543.210' is invalid using Float.
' '9.876.543,210' is invalid using Currency.
' '9.876.543,210' is invalid using Float.
' '98_76_54_32,19' is invalid using Currency.
' '98_76_54_32,19' is invalid using Float.
'
' Parsing using the nl-NL culture:
' ' 987.654E-2' is invalid using Currency.
' ' 987.654E-2' is invalid using Float.
' ' 987,654E-2' is invalid using Currency.
' 987,654E-2 (Float) -> 9.87654
' (98765,43210) (Currency) -> -98765.43
' '(98765,43210)' is invalid using Float.
' '9,876,543.210' is invalid using Currency.
' '9,876,543.210' is invalid using Float.
' 9.876.543,210 (Currency) -> 9876543
' '9.876.543,210' is invalid using Float.
' '98_76_54_32,19' is invalid using Currency.
' '98_76_54_32,19' is invalid using Float.
'
' Parsing using the Invariant culture:
' ' 987.654E-2' is invalid using Currency.
' 987.654E-2 (Float) -> 9.87654
' ' 987,654E-2' is invalid using Currency.
' ' 987,654E-2' is invalid using Float.
' (98765,43210) (Currency) -> -9.876543E+09
' '(98765,43210)' is invalid using Float.
' 9,876,543.210 (Currency) -> 9876543
' '9,876,543.210' is invalid using Float.
' '9.876.543,210' is invalid using Currency.
' '9.876.543,210' is invalid using Float.
' 98_76_54_32,19 (Currency) -> 9.876543E+09
' '98_76_54_32,19' is invalid using Float.
注解
在 .NET Core 3.0 及更高版本中,太大而无法表示的值将舍入到 PositiveInfinity IEEE 754 规范要求的 或 NegativeInfinity 。 在以前的版本中(包括 .NET Framework)中,分析太大而无法表示的值会导致失败。
参数 style
定义 (样式元素,如空格、千位分隔符和货币符号) ,这些元素在 参数中 s
允许分析操作成功。 它必须是 枚举中的位标志 NumberStyles 的组合。 不支持以下 NumberStyles 成员:
对于 s
指定的provider
区域性, 参数可以包含 NumberFormatInfo.PositiveInfinitySymbol、 NumberFormatInfo.NegativeInfinitySymbol或 NumberFormatInfo.NaNSymbol 。 根据 的值 style
,它还可能采用以下形式:
[ws][$] [sign][integral-digits,]integral-digits[.[fractional-digits]][E[sign]exponential-digits][ws]
以方括号 ([ 和 ]) 框起来的元素是可选的。 下表对每个元素进行了描述。
元素 | 说明 |
---|---|
ws | 一系列空白字符。 如果包含 标志,s 则开头会出现空格;如果style style 包含 NumberStyles.AllowTrailingWhite 标志,则它可能出现在 末尾s 。NumberStyles.AllowLeadingWhite |
$ | 区域性特定的货币符号。 它在字符串中的位置由 NumberFormatInfo.CurrencyNegativePattern 当前区域性的 和 NumberFormatInfo.CurrencyPositivePattern 属性定义。 如果style 包含 NumberStyles.AllowCurrencySymbol 标志,则当前区域性的货币符号可以出现在 中s 。 |
sign | 负号符号 ( ) 或正号符号 (+) 。 如果包含 标志,则符号可以出现在 的s 开头,如果style 包含 NumberStyles.AllowTrailingSign 标志,则它可显示在 的末尾s 。NumberStyles.AllowLeadingSignstyle 如果style 包含 标志,NumberStyles.AllowParentheses则可以在 中使用s 括号来指示负值。 |
integral-digits | 一系列数字,范围从 0 到 9,指定数字的整数部分。 如果字符串包含小数位数元素,则整数位元素可能不存在。 |
, | 区域性特定的组分隔符。 如果style 包含 NumberStyles.AllowThousands 标志,则当前区域性的组分隔符可以出现在 中s |
. | 区域性特定的小数点符号。 如果style 包含 NumberStyles.AllowDecimalPoint 标志,则当前区域性的小数点符号可以出现在 中s 。 |
fractional-digits | 一系列数字,范围从 0 到 9,指定数字的小数部分。 如果style 包含 标志,NumberStyles.AllowDecimalPoint则可以在 中s 显示小数位数。 |
E | “e”或“E”字符,指示该值以指数 (科学) 表示法表示。 如果style 包含 标志,则 s 参数可以表示指数表示法中的NumberStyles.AllowExponent数字。 |
exponential-digits | 指定指数的一系列数字,范围为 0 到 9。 |
注意
分析操作将忽略中 s
任何 (U+0000) 字符的终止 NUL,而不考虑 参数的值 style
。
仅包含数字的字符串 (对应于 NumberStyles.None 样式) 如果位于类型范围内 Single ,则始终会成功分析。 其余 System.Globalization.NumberStyles 成员控制输入字符串中可能存在但不需要存在的元素。 下表指示各个 NumberStyles 标志如何影响 中可能存在 s
的元素。
NumberStyles 值 | 除数字外允许的 s 元素 |
---|---|
None | 仅限 整型数字 元素。 |
AllowDecimalPoint | 小数点 (.) 和 小数位数 元素。 |
AllowExponent | 指示指数表示法的“e”或“E”字符。 此标志本身支持 数字E数字形式的值;需要其他标志才能成功分析包含正数或负号和小数点符号等元素的字符串。 |
AllowLeadingWhite | 开头的 s ws 元素。 |
AllowTrailingWhite | 位于 末尾的 s ws 元素。 |
AllowLeadingSign | 开头的s 符号元素。 |
AllowTrailingSign | 末尾的s 符号元素。 |
AllowParentheses | 用括号括住数值的 符号 元素。 |
AllowThousands | 千位分隔符 (,) 元素。 |
AllowCurrencySymbol | $) 元素 (货币。 |
Currency | 所有元素。 但是, s 不能表示十六进制数或指数表示法中的数字。 |
Float | 位于 开头或末尾的 s ws元素,符号位于 开头,s 小数点 (.) 符号。 参数 s 还可以使用指数表示法。 |
Number |
ws 、 sign 、千分隔符 (,) 和小数点 (.) 元素。 |
Any | 所有元素。 但是, s 不能表示十六进制数。 |
参数 provider
是实现 IFormatProvider 。 其 GetFormat 方法返回一个 NumberFormatInfo 对象,该对象提供有关 格式的 value
区域性特定信息。 通常, provider
可以是以下任一项:
一个 CultureInfo 对象,表示提供数值格式信息的区域性。 其 GetFormat 方法返回 NumberFormatInfo 提供数值格式设置信息的 对象。
提供 NumberFormatInfo 格式设置信息的 对象。 (它的实现 GetFormat 只是返回自身。)
一个自定义对象,它实现 IFormatProvider 并使用 GetFormat 方法实例化并返回 NumberFormatInfo 提供格式设置信息的对象。
如果 provider
为 null
,则 NumberFormatInfo 使用当前区域性的 对象。
如果 s
已超过数据类型的范围Single,则方法在 .NET Framework 和 .NET Core 2.2 及更早版本上引发 OverflowException 。 在 .NET Core 3.0 及更高版本中,如果 s
小于 Single.MinValue 且 Single.PositiveInfinitys
大于 Single.MaxValue,则返回 Single.NegativeInfinity 。
如果在分析操作期间参数中 s
遇到分隔符,并且适用的货币或数字小数分隔符和组分隔符相同,则分析操作假定分隔符是小数点分隔符,而不是组分隔符。 有关分隔符的详细信息,请参阅 CurrencyDecimalSeparator、 NumberDecimalSeparator、 CurrencyGroupSeparator和 NumberGroupSeparator。
另请参阅
适用于
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈