Converts the string representation of a number in a specified style and culture-specific format to its single-precision floating-point number equivalent.
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
Parameters
A string that contains a number to convert.
A bitwise combination of enumeration values that indicates the style elements that can be present in s
. A typical value to specify is Float combined with AllowThousands.
An object that supplies culture-specific formatting information about s
.
A single-precision floating-point number equivalent to the numeric value or symbol specified in s
.
s
does not represent a numeric value.
The following code example uses the Parse(String, NumberStyles, IFormatProvider) method to parse the string representations of Single values. Each string in an array is parsed using the formatting conventions of the en-US, nl-NL, and a custom culture. The custom culture defines its group separator symbol as the underscore ("_") and its group size as two.
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.
Remarks
In .NET Core 3.0 and later, values that are too large to represent are rounded to PositiveInfinity or NegativeInfinity as required by the IEEE 754 specification. In prior versions, including .NET Framework, parsing a value that was too large to represent resulted in failure.
The style
parameter defines the style elements (such as white space, thousands separators, and currency symbols) that are allowed in the s
parameter for the parse operation to succeed. It must be a combination of bit flags from the NumberStyles enumeration. The following NumberStyles members are not supported:
The s
parameter can contain NumberFormatInfo.PositiveInfinitySymbol, NumberFormatInfo.NegativeInfinitySymbol, or NumberFormatInfo.NaNSymbol for the culture specified by provider
. Depending on the value of style
, it can also take the form:
[ws] [$] [sign][integral-digits,]integral-digits[.[fractional-digits]][E[sign]exponential-digits][ws]
Elements framed in square brackets ([ and ]) are optional. The following table describes each element.
Element Description ws A series of white-space characters. White space can appear at the beginning ofs
if style
includes the NumberStyles.AllowLeadingWhite flag, and it can appear at the end of s
if style
includes the NumberStyles.AllowTrailingWhite flag. $ A culture-specific currency symbol. Its position in the string is defined by the NumberFormatInfo.CurrencyNegativePattern and NumberFormatInfo.CurrencyPositivePattern properties of the current culture. The current culture's currency symbol can appear in s
if style
includes the NumberStyles.AllowCurrencySymbol flag. sign A negative sign symbol (-) or a positive sign symbol (+). The sign can appear at the beginning of s
if style
includes the NumberStyles.AllowLeadingSign flag, and it can appear at the end of s
if style
includes the NumberStyles.AllowTrailingSign flag. Parentheses can be used in s
to indicate a negative value if style
includes the NumberStyles.AllowParentheses flag. integral-digits A series of digits ranging from 0 to 9 that specify the integral part of the number. The integral-digits element can be absent if the string contains the fractional-digits element. , A culture-specific group separator. The current culture's group separator symbol can appear in s
if style
includes the NumberStyles.AllowThousands flag . A culture-specific decimal point symbol. The current culture's decimal point symbol can appear in s
if style
includes the NumberStyles.AllowDecimalPoint flag. fractional-digits A series of digits ranging from 0 to 9 that specify the fractional part of the number. Fractional digits can appear in s
if style
includes the NumberStyles.AllowDecimalPoint flag. E The "e" or "E" character, which indicates that the value is represented in exponential (scientific) notation. The s
parameter can represent a number in exponential notation if style
includes the NumberStyles.AllowExponent flag. exponential-digits A series of digits ranging from 0 to 9 that specify an exponent.
Note
Any terminating NUL (U+0000) characters in s
are ignored by the parsing operation, regardless of the value of the style
argument.
A string with digits only (which corresponds to the NumberStyles.None style) always parses successfully if it is in the range of the Single type. The remaining System.Globalization.NumberStyles members control elements that may be present, but are not required to be present, in the input string. The following table indicates how individual NumberStyles flags affect the elements that may be present in s
.
s
in addition to digits None The integral-digits element only. AllowDecimalPoint The decimal point (.) and fractional-digits elements. AllowExponent The "e" or "E" character, which indicates exponential notation. This flag by itself supports values in the form digitsEdigits; additional flags are needed to successfully parse strings with such elements as positive or negative signs and decimal point symbols. AllowLeadingWhite The ws element at the beginning of s
. AllowTrailingWhite The ws element at the end of s
. AllowLeadingSign The sign element at the beginning of s
. AllowTrailingSign The sign element at the end of s
. AllowParentheses The sign element in the form of parentheses enclosing the numeric value. AllowThousands The thousands separator (,) element. AllowCurrencySymbol The currency ($) element. Currency All elements. However, s
cannot represent a hexadecimal number or a number in exponential notation. Float The ws element at the beginning or end of s
, sign at the beginning of s
, and the decimal point (.) symbol. The s
parameter can also use exponential notation. Number The ws
, sign
, thousands separator (,) and decimal point (.) elements. Any All elements. However, s
cannot represent a hexadecimal number.
The provider
parameter is an IFormatProvider implementation. Its GetFormat method returns a NumberFormatInfo object that provides culture-specific information about the format of value
. Typically, provider
can be any one of the following:
A CultureInfo object that represents the culture that provides numeric formatting information. Its GetFormat method returns the NumberFormatInfo object that provides numeric formatting information.
A NumberFormatInfo object that provides formatting information. (Its implementation of GetFormat just returns itself.)
A custom object that implements IFormatProvider and uses the GetFormat method to instantiate and return the NumberFormatInfo object that provides formatting information.
If provider
is null
, the NumberFormatInfo object for the current culture is used.
If s
is out of range of the Single data type, the method throws an OverflowException on .NET Framework and .NET Core 2.2 and earlier versions. On .NET Core 3.0 and later versions, it returns Single.NegativeInfinity if s
is less than Single.MinValue and Single.PositiveInfinity if s
is greater than Single.MaxValue.
If a separator is encountered in the s
parameter during a parse operation, and the applicable currency or number decimal and group separators are the same, the parse operation assumes that the separator is a decimal separator rather than a group separator. For more information about separators, see CurrencyDecimalSeparator, NumberDecimalSeparator, CurrencyGroupSeparator, and NumberGroupSeparator.
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