Udostępnij za pośrednictwem


UInt64.Parse Metoda

Definicja

Konwertuje reprezentację ciągu liczby na 64-bitową liczbę całkowitą bez znaku.

Przeciążenia

Nazwa Opis
Parse(String, NumberStyles, IFormatProvider)

Konwertuje reprezentację ciągu liczby w określonym stylu i formacie specyficznym dla kultury na jego 64-bitową liczbę całkowitą bez znaku.

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

Konwertuje reprezentację zakresu liczby w określonym stylu i formacie specyficznym dla kultury na jego 64-bitową liczbę całkowitą bez znaku.

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

Analizuje zakres znaków UTF-8 w wartość.

Parse(String, IFormatProvider)

Konwertuje reprezentację ciągu liczby w określonym formacie specyficznym dla kultury na jego 64-bitowy niepodpisany odpowiednik liczb całkowitych.

Parse(ReadOnlySpan<Char>, IFormatProvider)

Analizuje zakres znaków w wartości.

Parse(ReadOnlySpan<Byte>, IFormatProvider)

Analizuje zakres znaków UTF-8 w wartość.

Parse(String)

Konwertuje reprezentację ciągu liczby na 64-bitową liczbę całkowitą bez znaku.

Parse(String, NumberStyles)

Konwertuje reprezentację ciągu liczby w określonym stylu na jej 64-bitowy odpowiednik liczby całkowitej bez znaku.

Parse(String, NumberStyles, IFormatProvider)

Źródło:
UInt64.cs
Źródło:
UInt64.cs
Źródło:
UInt64.cs
Źródło:
UInt64.cs
Źródło:
UInt64.cs

Ważne

Ten interfejs API nie jest zgodny ze specyfikacją CLS.

Alternatywa zgodna ze specyfikacją CLS
System.Decimal.Parse(String)

Konwertuje reprezentację ciągu liczby w określonym stylu i formacie specyficznym dla kultury na jego 64-bitową liczbę całkowitą bez znaku.

public:
 static System::UInt64 Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
public:
 static System::UInt64 Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider) = System::Numerics::INumberBase<System::UInt64>::Parse;
[System.CLSCompliant(false)]
public static ulong Parse(string s, System.Globalization.NumberStyles style, IFormatProvider provider);
public static ulong Parse(string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
[System.CLSCompliant(false)]
public static ulong Parse(string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
[<System.CLSCompliant(false)>]
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> uint64
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> uint64
Public Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As ULong

Parametry

s
String

Ciąg reprezentujący liczbę do przekonwertowania. Ciąg jest interpretowany przy użyciu stylu określonego style przez parametr .

style
NumberStyles

Bitowa kombinacja wartości wyliczenia wskazująca elementy stylu, które mogą być obecne w elemecie s. Typową wartością do określenia jest Integer.

provider
IFormatProvider

Obiekt, który dostarcza informacje o formatowaniu specyficznym dla kultury.s

Zwraca

64-bitowa liczba całkowita bez znaku równoważna liczbie określonej w elemencie s.

Implementuje

Atrybuty

Wyjątki

Parametr s jest null.

style nie jest wartością NumberStyles .

-lub-

style nie jest kombinacją AllowHexSpecifier wartości i HexNumber .

Parametr s nie jest w formacie zgodnym z parametrem style.

Parametr s reprezentuje liczbę mniejszą niż UInt64.MinValue lub większa niż UInt64.MaxValue.

-lub-

s zawiera cyfry niezerowe, ułamkowe.

Przykłady

W poniższym przykładzie użyto Parse(String, NumberStyles, IFormatProvider) metody , aby przekonwertować różne reprezentacje ciągów liczb na 64-bitowe niepodpisane wartości całkowite.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] cultureNames= { "en-US", "fr-FR" };
      NumberStyles[] styles= { NumberStyles.Integer,
                               NumberStyles.Integer | NumberStyles.AllowDecimalPoint };
      string[] values = { "170209", "+170209.0", "+170209,0", "-103214.00",
                                 "-103214,00", "104561.1", "104561,1" };
      
      // Parse strings using each culture
      foreach (string cultureName in cultureNames)
      {
         CultureInfo ci = new CultureInfo(cultureName);
         Console.WriteLine("Parsing strings using the {0} culture", 
                           ci.DisplayName);
         // Use each style.
         foreach (NumberStyles style in styles)
         {
            Console.WriteLine("   Style: {0}", style.ToString());
            // Parse each numeric string.
            foreach (string value in values)
            {
               try {
                  Console.WriteLine("      Converted '{0}' to {1}.", value,
                                    UInt64.Parse(value, style, ci));
               }
               catch (FormatException) {
                  Console.WriteLine("      Unable to parse '{0}'.", value);
               }      
               catch (OverflowException) {
                  Console.WriteLine("      '{0}' is out of range of the UInt64 type.",
                                    value);
               }
            }
         }
      }                                    
   }
}
// The example displays the following output:
//       Style: Integer
//          Converted '170209' to 170209.
//          Unable to parse '+170209.0'.
//          Unable to parse '+170209,0'.
//          Unable to parse '-103214.00'.
//          Unable to parse '-103214,00'.
//          Unable to parse '104561.1'.
//          Unable to parse '104561,1'.
//       Style: Integer, AllowDecimalPoint
//          Converted '170209' to 170209.
//          Converted '+170209.0' to 170209.
//          Unable to parse '+170209,0'.
//          '-103214.00' is out of range of the UInt64 type.
//          Unable to parse '-103214,00'.
//          '104561.1' is out of range of the UInt64 type.
//          Unable to parse '104561,1'.
//    Parsing strings using the French (France) culture
//       Style: Integer
//          Converted '170209' to 170209.
//          Unable to parse '+170209.0'.
//          Unable to parse '+170209,0'.
//          Unable to parse '-103214.00'.
//          Unable to parse '-103214,00'.
//          Unable to parse '104561.1'.
//          Unable to parse '104561,1'.
//       Style: Integer, AllowDecimalPoint
//          Converted '170209' to 170209.
//          Unable to parse '+170209.0'.
//          Converted '+170209,0' to 170209.
//          Unable to parse '-103214.00'.
//          '-103214,00' is out of range of the UInt64 type.
//          Unable to parse '104561.1'.
//          '104561,1' is out of range of the UInt64 type.
open System
open System.Globalization

let cultureNames = [| "en-US"; "fr-FR" |]
let styles = [| NumberStyles.Integer; NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint |]
let values = 
    [| "170209"; "+170209.0"; "+170209,0"; "-103214.00"
       "-103214,00"; "104561.1"; "104561,1" |]

// Parse strings using each culture
for cultureName in cultureNames do
    let ci = CultureInfo cultureName
    printfn $"Parsing strings using the {ci.DisplayName} culture"
    // Use each style.
    for style in styles do
        printfn $"   Style: {style}"
        // Parse each numeric string.
        for value in values do
            try
                printfn $"      Converted '{value}' to {UInt64.Parse(value, style, ci)}."
            with
            | :? FormatException ->
                printfn $"      Unable to parse '{value}'."
            | :? OverflowException ->
                printfn $"      '{value}' is out of range of the UInt64 type."
// The example displays the following output:
//       Style: Integer
//          Converted '170209' to 170209.
//          Unable to parse '+170209.0'.
//          Unable to parse '+170209,0'.
//          Unable to parse '-103214.00'.
//          Unable to parse '-103214,00'.
//          Unable to parse '104561.1'.
//          Unable to parse '104561,1'.
//       Style: Integer, AllowDecimalPoint
//          Converted '170209' to 170209.
//          Converted '+170209.0' to 170209.
//          Unable to parse '+170209,0'.
//          '-103214.00' is out of range of the UInt64 type.
//          Unable to parse '-103214,00'.
//          '104561.1' is out of range of the UInt64 type.
//          Unable to parse '104561,1'.
//    Parsing strings using the French (France) culture
//       Style: Integer
//          Converted '170209' to 170209.
//          Unable to parse '+170209.0'.
//          Unable to parse '+170209,0'.
//          Unable to parse '-103214.00'.
//          Unable to parse '-103214,00'.
//          Unable to parse '104561.1'.
//          Unable to parse '104561,1'.
//       Style: Integer, AllowDecimalPoint
//          Converted '170209' to 170209.
//          Unable to parse '+170209.0'.
//          Converted '+170209,0' to 170209.
//          Unable to parse '-103214.00'.
//          '-103214,00' is out of range of the UInt64 type.
//          Unable to parse '104561.1'.
//          '104561,1' is out of range of the UInt64 type.
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim cultureNames() As String = { "en-US", "fr-FR" }
      Dim styles() As NumberStyles = { NumberStyles.Integer, _
                                       NumberStyles.Integer Or NumberStyles.AllowDecimalPoint }
      Dim values() As String = { "170209", "+170209.0", "+170209,0", "-103214.00", _
                                 "-103214,00", "104561.1", "104561,1" }
      
      ' Parse strings using each culture
      For Each cultureName As String In cultureNames
         Dim ci As New CultureInfo(cultureName)
         Console.WriteLine("Parsing strings using the {0} culture", ci.DisplayName)
         ' Use each style.
         For Each style As NumberStyles In styles
            Console.WriteLine("   Style: {0}", style.ToString())
            ' Parse each numeric string.
            For Each value As String In values
               Try
                  Console.WriteLine("      Converted '{0}' to {1}.", value, _
                                    UInt64.Parse(value, style, ci))
               Catch e As FormatException
                  Console.WriteLine("      Unable to parse '{0}'.", value)   
               Catch e As OverflowException
                  Console.WriteLine("      '{0}' is out of range of the UInt64 type.", _
                                    value)         
               End Try
            Next
         Next
      Next                                    
   End Sub
End Module
' The example displays the following output:
'       Style: Integer
'          Converted '170209' to 170209.
'          Unable to parse '+170209.0'.
'          Unable to parse '+170209,0'.
'          Unable to parse '-103214.00'.
'          Unable to parse '-103214,00'.
'          Unable to parse '104561.1'.
'          Unable to parse '104561,1'.
'       Style: Integer, AllowDecimalPoint
'          Converted '170209' to 170209.
'          Converted '+170209.0' to 170209.
'          Unable to parse '+170209,0'.
'          '-103214.00' is out of range of the UInt64 type.
'          Unable to parse '-103214,00'.
'          '104561.1' is out of range of the UInt64 type.
'          Unable to parse '104561,1'.
'    Parsing strings using the French (France) culture
'       Style: Integer
'          Converted '170209' to 170209.
'          Unable to parse '+170209.0'.
'          Unable to parse '+170209,0'.
'          Unable to parse '-103214.00'.
'          Unable to parse '-103214,00'.
'          Unable to parse '104561.1'.
'          Unable to parse '104561,1'.
'       Style: Integer, AllowDecimalPoint
'          Converted '170209' to 170209.
'          Unable to parse '+170209.0'.
'          Converted '+170209,0' to 170209.
'          Unable to parse '-103214.00'.
'          '-103214,00' is out of range of the UInt64 type.
'          Unable to parse '104561.1'.
'          '104561,1' is out of range of the UInt64 type.

Uwagi

Parametr style definiuje elementy stylu (takie jak biały znak lub symbol znaku dodatniego lub ujemnego), które są dozwolone w parametrze s dla operacji analizowania, która powiedzie się. Musi to być kombinacja flag bitowych z NumberStyles wyliczenia.

W zależności od wartości styleparametr może s zawierać następujące elementy:

[ws][$][znak]cyfry[.fractional_digits][E[znak]exponential_digits][ws]

Elementy w nawiasach kwadratowych ([ i ]) są opcjonalne. Jeśli style parametr zawiera NumberStyles.AllowHexSpecifierparametr , s może zawierać następujące elementy:

[ws]hexdigits[ws]

W poniższej tabeli opisano każdy element.

Pierwiastek Opis
Ws Opcjonalne białe znaki. Białe znaki mogą pojawić się na początku s , jeśli style zawiera flagę NumberStyles.AllowLeadingWhite , i może pojawić się na końcu s , jeśli style zawiera flagę NumberStyles.AllowTrailingWhite .
$ Symbol waluty specyficznej dla kultury. Jego pozycja w ciągu jest definiowana przez CurrencyPositivePattern właściwość NumberFormatInfo obiektu, który jest zwracany przez GetFormat metodę parametru provider . Symbol waluty może pojawić się, s jeśli style zawiera flagę NumberStyles.AllowCurrencySymbol .
podpis Opcjonalny znak. (Metoda zgłasza OverflowException wyjątek if s zawiera znak ujemny i reprezentuje liczbę niezerową). Znak może pojawić się na początku s , jeśli style zawiera flagę NumberStyles.AllowLeadingSign , i może pojawić się na końcu s , jeśli style zawiera flagę NumberStyles.AllowTrailingSign . Nawiasy mogą służyć do wskazywania wartości ujemnej, s jeśli style zawiera flagę NumberStyles.AllowParentheses .
Cyfr Sekwencja cyfr z zakresu od 0 do 9.
. Symbol separatora dziesiętnego specyficznego dla kultury. Symbol dziesiętny bieżącej kultury może pojawić się, s jeśli style zawiera flagę NumberStyles.AllowDecimalPoint .
fractional_digits Co najmniej jedno wystąpienie cyfry 0–9, jeśli style zawiera flagę NumberStyles.AllowExponent lub co najmniej jedno wystąpienie cyfry 0, jeśli nie. Cyfry ułamkowe mogą być wyświetlane tylko wtedy s , gdy style zawiera flagę NumberStyles.AllowDecimalPoint .
E Znak "e" lub "E", który wskazuje, że wartość jest reprezentowana w notacji wykładniczej (naukowej). Parametr s może reprezentować liczbę w notacji wykładniczej, jeśli style zawiera flagę NumberStyles.AllowExponent .
exponential_digits Sekwencja cyfr z zakresu od 0 do 9. Parametr s może reprezentować liczbę w notacji wykładniczej, jeśli style zawiera flagę NumberStyles.AllowExponent .
hexdigits Sekwencja cyfr szesnastkowa od 0 do f lub od 0 do F.

Nuta

Wszystkie znaki NUL (U+0000) w obiekcie s są ignorowane przez operację analizowania, niezależnie od wartości argumentu style .

Ciąg z cyframi dziesiętnymi (który odpowiada NumberStyles.None stylowi) zawsze analizuje się pomyślnie. Większość pozostałych NumberStyles elementów członkowskich kontroluje elementy, które mogą być obecne, ale nie muszą być obecne, w tym ciągu wejściowym. W poniższej tabeli przedstawiono, jak poszczególne NumberStyles elementy członkowskie wpływają na elementy, które mogą być obecne w programie s.

Wartości nieskładne NumberStyles Elementy dozwolone s oprócz cyfr
NumberStyles.None Tylko cyfry dziesiętne.
NumberStyles.AllowDecimalPoint Elementy dziesiętne (.) i fractional_digits . Jeśli jednak styl nie zawiera flagi NumberStyles.AllowExponent , fractional_digits musi zawierać tylko jedną lub więcej cyfr, w przeciwnym razie OverflowException jest zgłaszana wartość .
NumberStyles.AllowExponent Znak "e" lub "E", który wskazuje notację wykładniczą wraz z exponential_digits.
NumberStyles.AllowLeadingWhite Element ws na początku elementu s.
NumberStyles.AllowTrailingWhite Element ws na końcu elementu s.
NumberStyles.AllowLeadingSign Znak przed cyframi.
NumberStyles.AllowTrailingSign Znak po cyfrach.
NumberStyles.AllowParentheses Nawiasy przed cyframi i po, aby wskazać wartość ujemną.
NumberStyles.AllowThousands Element separatora grup (,).
NumberStyles.AllowCurrencySymbol Element currency ($).

Jeśli flaga NumberStyles.AllowHexSpecifier jest używana, s musi być wartością szesnastkową. Prawidłowe znaki szesnastkowe to 0-9, A-F i a-f. Prefiks taki jak "0x" nie jest obsługiwany i powoduje niepowodzenie operacji analizowania. Jedynymi innymi flagami, które można połączyć z nim, są NumberStyles.AllowLeadingWhite i NumberStyles.AllowTrailingWhite. (Wyliczenie NumberStyles zawiera styl liczby złożonej, NumberStyles.HexNumber, który zawiera obie flagi odstępu).

Nuta

s Jeśli parametr jest reprezentacją ciągu liczby szesnastkowej, nie może być poprzedzony żadną dekoracją (taką jak 0x lub &h), która odróżnia ją jako liczbę szesnastkową. Powoduje to, że operacja analizy zgłasza wyjątek.

Parametr provider jest implementacją IFormatProvider , której GetFormat metoda zwraca NumberFormatInfo obiekt, który dostarcza informacji specyficznych dla kultury o formacie s. Istnieją trzy sposoby użycia parametru provider w celu podania niestandardowych informacji o formatowaniu do operacji analizowania:

  • Można przekazać rzeczywisty NumberFormatInfo obiekt, który zawiera informacje o formatowaniu. (Jego implementacja GetFormat po prostu zwraca się).

  • Można przekazać obiekt określający kulturę CultureInfo , której formatowanie ma być używane. Jego NumberFormat właściwość udostępnia informacje o formatowaniu.

  • Możesz przekazać implementację niestandardową IFormatProvider . Metoda GetFormat musi utworzyć wystąpienie i zwrócić NumberFormatInfo obiekt, który udostępnia informacje o formatowaniu.

Jeśli provider ma nullwartość , NumberFormatInfo używany jest obiekt bieżącej kultury.

Zobacz też

Dotyczy

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

Źródło:
UInt64.cs
Źródło:
UInt64.cs
Źródło:
UInt64.cs
Źródło:
UInt64.cs
Źródło:
UInt64.cs

Ważne

Ten interfejs API nie jest zgodny ze specyfikacją CLS.

Konwertuje reprezentację zakresu liczby w określonym stylu i formacie specyficznym dla kultury na jego 64-bitową liczbę całkowitą bez znaku.

public static ulong Parse(ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
[System.CLSCompliant(false)]
public static ulong Parse(ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider provider = default);
[System.CLSCompliant(false)]
public static ulong Parse(ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> uint64
[<System.CLSCompliant(false)>]
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> uint64
Public Shared Function Parse (s As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As ULong

Parametry

s
ReadOnlySpan<Char>

Zakres zawierający znaki reprezentujące liczbę do przekonwertowania. Zakres jest interpretowany przy użyciu stylu określonego style przez parametr .

style
NumberStyles

Bitowa kombinacja wartości wyliczenia wskazująca elementy stylu, które mogą być obecne w elemecie s. Typową wartością do określenia jest Integer.

provider
IFormatProvider

Obiekt, który dostarcza informacje o formatowaniu specyficznym dla kultury.s

Zwraca

64-bitowa liczba całkowita bez znaku równoważna liczbie określonej w elemencie s.

Implementuje

Atrybuty

Dotyczy

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

Źródło:
UInt64.cs
Źródło:
UInt64.cs
Źródło:
UInt64.cs
Źródło:
UInt64.cs

Analizuje zakres znaków UTF-8 w wartość.

public static ulong Parse(ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider -> uint64
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As ULong

Parametry

utf8Text
ReadOnlySpan<Byte>

Zakres znaków UTF-8 do przeanalizowania.

style
NumberStyles

Bitowa kombinacja stylów liczbowych, które mogą być obecne w pliku utf8Text.

provider
IFormatProvider

Obiekt, który udostępnia informacje o formatowaniu specyficznym dla kultury.utf8Text

Zwraca

Wynik analizowania utf8Text.

Implementuje

Dotyczy

Parse(String, IFormatProvider)

Źródło:
UInt64.cs
Źródło:
UInt64.cs
Źródło:
UInt64.cs
Źródło:
UInt64.cs
Źródło:
UInt64.cs

Ważne

Ten interfejs API nie jest zgodny ze specyfikacją CLS.

Alternatywa zgodna ze specyfikacją CLS
System.Decimal.Parse(String)

Konwertuje reprezentację ciągu liczby w określonym formacie specyficznym dla kultury na jego 64-bitowy niepodpisany odpowiednik liczb całkowitych.

public:
 static System::UInt64 Parse(System::String ^ s, IFormatProvider ^ provider);
public:
 static System::UInt64 Parse(System::String ^ s, IFormatProvider ^ provider) = IParsable<System::UInt64>::Parse;
[System.CLSCompliant(false)]
public static ulong Parse(string s, IFormatProvider provider);
public static ulong Parse(string s, IFormatProvider? provider);
[System.CLSCompliant(false)]
public static ulong Parse(string s, IFormatProvider? provider);
[<System.CLSCompliant(false)>]
static member Parse : string * IFormatProvider -> uint64
static member Parse : string * IFormatProvider -> uint64
Public Shared Function Parse (s As String, provider As IFormatProvider) As ULong

Parametry

s
String

Ciąg reprezentujący liczbę do przekonwertowania.

provider
IFormatProvider

Obiekt, który dostarcza informacje o formatowaniu specyficznym dla kultury.s

Zwraca

64-bitowa liczba całkowita bez znaku równoważna liczbie określonej w elemencie s.

Implementuje

Atrybuty

Wyjątki

Parametr s jest null.

Parametr s nie jest w poprawnym stylu.

Parametr s reprezentuje liczbę mniejszą niż UInt64.MinValue lub większa niż UInt64.MaxValue.

Przykłady

Poniższy przykład to procedura obsługi zdarzeń kliknięcia przycisku formularza internetowego. Używa tablicy zwracanej przez HttpRequest.UserLanguages właściwość w celu określenia ustawień regionalnych użytkownika. Następnie tworzy wystąpienie CultureInfo obiektu, który odpowiada tym ustawień regionalnych. Obiekt NumberFormatInfo , który należy do tego CultureInfo obiektu, jest następnie przekazywany do Parse(String, IFormatProvider) metody w celu przekonwertowania danych wejściowych użytkownika na UInt64 wartość.

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

Uwagi

To przeciążenie Parse(String, IFormatProvider) metody jest zwykle używane do konwertowania tekstu, który można sformatować na różne sposoby na UInt64 wartość. Na przykład można go użyć do przekonwertowania tekstu wprowadzonego przez użytkownika na pole tekstowe HTML na wartość liczbową.

Parametr s zawiera liczbę formularzy:

[ws] [znak]cyfry[ws]

Elementy w nawiasach kwadratowych ([ i ]) są opcjonalne. W poniższej tabeli opisano każdy element.

Pierwiastek Opis
Ws Opcjonalne białe znaki.
podpis Opcjonalny znak dodatni lub znak ujemny, jeśli s reprezentuje wartość zero.
Cyfr Sekwencja cyfr od 0 do 9.

Parametr s jest interpretowany przy użyciu NumberStyles.Integer stylu. Oprócz cyfr dziesiętnych niepodpisanej wartości całkowitej dozwolone są tylko spacje wiodące i końcowe wraz z znakiem wiodącym. (Jeśli znak ujemny jest obecny, s musi reprezentować wartość zero lub metoda zgłasza OverflowExceptionwartość .) Aby jawnie zdefiniować elementy stylu wraz z informacjami o formatowaniu specyficznymi dla kultury, które mogą być obecne w sprogramie , użyj Parse(String, NumberStyles, IFormatProvider) metody .

Parametr provider jest implementacją IFormatProvider , której GetFormat metoda zwraca NumberFormatInfo obiekt, który dostarcza informacji specyficznych dla kultury o formacie s. Istnieją trzy sposoby użycia parametru provider w celu podania niestandardowych informacji o formatowaniu do operacji analizowania:

  • Można przekazać rzeczywisty NumberFormatInfo obiekt, który zawiera informacje o formatowaniu. (Jego implementacja GetFormat po prostu zwraca się).

  • Można przekazać obiekt określający kulturę CultureInfo , której formatowanie ma być używane. Jego NumberFormat właściwość udostępnia informacje o formatowaniu.

  • Możesz przekazać implementację niestandardową IFormatProvider . Metoda GetFormat musi utworzyć wystąpienie i zwrócić NumberFormatInfo obiekt, który udostępnia informacje o formatowaniu.

Jeśli provider wartość to null, NumberFormatInfo używana jest wartość dla bieżącej kultury.

Zobacz też

Dotyczy

Parse(ReadOnlySpan<Char>, IFormatProvider)

Źródło:
UInt64.cs
Źródło:
UInt64.cs
Źródło:
UInt64.cs
Źródło:
UInt64.cs
Źródło:
UInt64.cs

Analizuje zakres znaków w wartości.

public:
 static System::UInt64 Parse(ReadOnlySpan<char> s, IFormatProvider ^ provider) = ISpanParsable<System::UInt64>::Parse;
public static ulong Parse(ReadOnlySpan<char> s, IFormatProvider? provider);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> uint64
Public Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As ULong

Parametry

s
ReadOnlySpan<Char>

Zakres znaków do przeanalizowania.

provider
IFormatProvider

Obiekt, który udostępnia informacje o formatowaniu specyficznym dla kultury.s

Zwraca

Wynik analizowania s.

Implementuje

Dotyczy

Parse(ReadOnlySpan<Byte>, IFormatProvider)

Źródło:
UInt64.cs
Źródło:
UInt64.cs
Źródło:
UInt64.cs
Źródło:
UInt64.cs

Analizuje zakres znaków UTF-8 w wartość.

public:
 static System::UInt64 Parse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider) = IUtf8SpanParsable<System::UInt64>::Parse;
public static ulong Parse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);
static member Parse : ReadOnlySpan<byte> * IFormatProvider -> uint64
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider) As ULong

Parametry

utf8Text
ReadOnlySpan<Byte>

Zakres znaków UTF-8 do przeanalizowania.

provider
IFormatProvider

Obiekt, który udostępnia informacje o formatowaniu specyficznym dla kultury.utf8Text

Zwraca

Wynik analizowania utf8Text.

Implementuje

Dotyczy

Parse(String)

Źródło:
UInt64.cs
Źródło:
UInt64.cs
Źródło:
UInt64.cs
Źródło:
UInt64.cs
Źródło:
UInt64.cs

Ważne

Ten interfejs API nie jest zgodny ze specyfikacją CLS.

Alternatywa zgodna ze specyfikacją CLS
System.Decimal.Parse(String)

Konwertuje reprezentację ciągu liczby na 64-bitową liczbę całkowitą bez znaku.

public:
 static System::UInt64 Parse(System::String ^ s);
[System.CLSCompliant(false)]
public static ulong Parse(string s);
public static ulong Parse(string s);
[<System.CLSCompliant(false)>]
static member Parse : string -> uint64
static member Parse : string -> uint64
Public Shared Function Parse (s As String) As ULong

Parametry

s
String

Ciąg reprezentujący liczbę do przekonwertowania.

Zwraca

64-bitowa liczba całkowita bez znaku odpowiadająca liczbie zawartej w elemencie s.

Atrybuty

Wyjątki

Parametr s jest null.

Parametr s nie jest w poprawnym formacie.

Parametr s reprezentuje liczbę mniejszą niż UInt64.MinValue lub większa niż UInt64.MaxValue.

Przykłady

W poniższym przykładzie użyto Parse metody , aby przeanalizować tablicę wartości ciągu.

string[] values = { "+13230", "-0", "1,390,146", "$190,235,421,127",
                    "0xFA1B", "163042", "-10", "14065839182",
                    "16e07", "134985.0", "-12034" };
foreach (string value in values)
{
   try {
      ulong number = UInt64.Parse(value); 
      Console.WriteLine("{0} --> {1}", value, number);
   }
   catch (FormatException) {
      Console.WriteLine("{0}: Bad Format", value);
   }   
   catch (OverflowException) {
      Console.WriteLine("{0}: Overflow", value);   
   }  
}
// The example displays the following output:
//       +13230 --> 13230
//       -0 --> 0
//       1,390,146: Bad Format
//       $190,235,421,127: Bad Format
//       0xFA1B: Bad Format
//       163042 --> 163042
//       -10: Overflow
//       14065839182 --> 14065839182
//       16e07: Bad Format
//       134985.0: Bad Format
//       -12034: Overflow
let values = 
    [| "+13230"; "-0"; "1,390,146"; "$190,235,421,127"
       "0xFA1B"; "163042"; "-10"; "14065839182"
       "16e07"; "134985.0"; "-12034" |]
for value in values do
    try 
        let number = UInt64.Parse value 
        printfn $"{value} --> {number}"
    with
    | :? FormatException ->
        printfn $"{value}: Bad Format"
    | :? OverflowException ->
        printfn $"{value}: Overflow"
// The example displays the following output:
//       +13230 --> 13230
//       -0 --> 0
//       1,390,146: Bad Format
//       $190,235,421,127: Bad Format
//       0xFA1B: Bad Format
//       163042 --> 163042
//       -10: Overflow
//       14065839182 --> 14065839182
//       16e07: Bad Format
//       134985.0: Bad Format
//       -12034: Overflow
Dim values() As String = { "+13230", "-0", "1,390,146", "$190,235,421,127", _
                           "0xFA1B", "163042", "-10", "14065839182", _
                           "16e07", "134985.0", "-12034" }
For Each value As String In values
   Try
      Dim number As ULong = UInt64.Parse(value) 
      Console.WriteLine("{0} --> {1}", value, number)
   Catch e As FormatException
      Console.WriteLine("{0}: Bad Format", value)
   Catch e As OverflowException
      Console.WriteLine("{0}: Overflow", value)   
   End Try  
Next
' The example displays the following output:
'       +13230 --> 13230
'       -0 --> 0
'       1,390,146: Bad Format
'       $190,235,421,127: Bad Format
'       0xFA1B: Bad Format
'       163042 --> 163042
'       -10: Overflow
'       14065839182 --> 14065839182
'       16e07: Bad Format
'       134985.0: Bad Format
'       -12034: Overflow

Uwagi

Parametr s powinien być ciągiem reprezentującym liczbę w poniższym formularzu.

[ws][podpis]digits[ws]

Elementy w nawiasach kwadratowych ([ i ]) są opcjonalne. W poniższej tabeli opisano każdy element.

Pierwiastek Opis
Ws Opcjonalne białe znaki.
podpis Opcjonalny znak. Prawidłowe znaki są określane przez NumberFormatInfo.NegativeSign właściwości i NumberFormatInfo.PositiveSign bieżącej kultury. Jednak symbol znaku ujemnego może być używany tylko z zerem; w przeciwnym razie metoda zgłasza błąd OverflowException.
Cyfr Sekwencja cyfr od 0 do 9. Wszystkie zera wiodące są ignorowane.

Nuta

Ciąg określony przez s parametr jest interpretowany przy użyciu NumberStyles.Integer stylu. Nie może zawierać żadnych separatorów grup ani separatora dziesiętnego i nie może mieć części dziesiętnej.

Parametr s jest analizowany przy użyciu informacji o formatowaniu w obiekcie zainicjowanym System.Globalization.NumberFormatInfo dla bieżącej kultury systemu. Aby uzyskać więcej informacji, zobacz NumberFormatInfo.CurrentInfo. Aby przeanalizować ciąg przy użyciu informacji o formatowaniu określonej kultury, użyj Parse(String, IFormatProvider) metody .

Zobacz też

Dotyczy

Parse(String, NumberStyles)

Źródło:
UInt64.cs
Źródło:
UInt64.cs
Źródło:
UInt64.cs
Źródło:
UInt64.cs
Źródło:
UInt64.cs

Ważne

Ten interfejs API nie jest zgodny ze specyfikacją CLS.

Alternatywa zgodna ze specyfikacją CLS
System.Decimal.Parse(String)

Konwertuje reprezentację ciągu liczby w określonym stylu na jej 64-bitowy odpowiednik liczby całkowitej bez znaku.

public:
 static System::UInt64 Parse(System::String ^ s, System::Globalization::NumberStyles style);
[System.CLSCompliant(false)]
public static ulong Parse(string s, System.Globalization.NumberStyles style);
public static ulong Parse(string s, System.Globalization.NumberStyles style);
[<System.CLSCompliant(false)>]
static member Parse : string * System.Globalization.NumberStyles -> uint64
static member Parse : string * System.Globalization.NumberStyles -> uint64
Public Shared Function Parse (s As String, style As NumberStyles) As ULong

Parametry

s
String

Ciąg reprezentujący liczbę do przekonwertowania. Ciąg jest interpretowany przy użyciu stylu określonego style przez parametr .

style
NumberStyles

Bitowa kombinacja wartości wyliczenia, która określa dozwolony format s. Typową wartością do określenia jest Integer.

Zwraca

64-bitowa liczba całkowita bez znaku równoważna liczbie określonej w elemencie s.

Atrybuty

Wyjątki

Parametr s jest null.

style nie jest wartością NumberStyles .

-lub-

style nie jest kombinacją AllowHexSpecifier wartości i HexNumber .

Parametr s nie jest w formacie zgodnym z parametrem style.

Parametr s reprezentuje liczbę mniejszą niż UInt64.MinValue lub większa niż UInt64.MaxValue.

-lub-

s zawiera cyfry niezerowe, ułamkowe.

Przykłady

Poniższy przykład próbuje przeanalizować każdy element w tablicy ciągów NumberStyles przy użyciu kilku wartości.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] values= { " 214309 ", "1,064,181", "(0)", "10241+", " + 21499 ", 
                         " +21499 ", "122153.00", "1e03ff", "91300.0e-2" };
      NumberStyles whitespace =  NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite;
      NumberStyles[] styles= { NumberStyles.None, whitespace, 
                               NumberStyles.AllowLeadingSign | NumberStyles.AllowTrailingSign | whitespace, 
                               NumberStyles.AllowThousands | NumberStyles.AllowCurrencySymbol, 
                               NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint };

      // Attempt to convert each number using each style combination.
      foreach (string value in values)
      {
         Console.WriteLine("Attempting to convert '{0}':", value);
         foreach (NumberStyles style in styles)
         {
            try {
               ulong number = UInt64.Parse(value, style);
               Console.WriteLine("   {0}: {1}", style, number);
            }   
            catch (FormatException) {
               Console.WriteLine("   {0}: Bad Format", style);
            }   
            catch (OverflowException)
            {
               Console.WriteLine("   {0}: Overflow", value);         
            }         
         }
         Console.WriteLine();
      }
   }
}
// The example displays the following output:
//    Attempting to convert ' 214309 ':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: 214309
//       Integer, AllowTrailingSign: 214309
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '1,064,181':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: 1064181
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '(0)':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '10241+':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: 10241
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert ' + 21499 ':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert ' +21499 ':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: 21499
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '122153.00':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: 122153
//    
//    Attempting to convert '1e03ff':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '91300.0e-2':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: 913
open System
open System.Globalization

let values =
    [| " 214309 "; "1,064,181"; "(0)"; "10241+"; " + 21499 " 
       " +21499 "; "122153.00"; "1e03ff"; "91300.0e-2" |]
let whitespace =  NumberStyles.AllowLeadingWhite ||| NumberStyles.AllowTrailingWhite
let styles =
    [| NumberStyles.None; whitespace 
       NumberStyles.AllowLeadingSign ||| NumberStyles.AllowTrailingSign ||| whitespace 
       NumberStyles.AllowThousands ||| NumberStyles.AllowCurrencySymbol 
       NumberStyles.AllowExponent ||| NumberStyles.AllowDecimalPoint |]

// Attempt to convert each number using each style combination.
for value in values do
    printfn $"Attempting to convert '{value}':"
    for style in styles do
        try
            let number = UInt64.Parse(value, style)
            printfn $"   {style}: {number}"
        with
        | :? FormatException ->
            printfn $"   {style}: Bad Format"
        | :? OverflowException ->
            printfn $"   {value}: Overflow"
    printfn ""
// The example displays the following output:
//    Attempting to convert ' 214309 ':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: 214309
//       Integer, AllowTrailingSign: 214309
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '1,064,181':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: 1064181
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '(0)':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '10241+':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: 10241
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert ' + 21499 ':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert ' +21499 ':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: 21499
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '122153.00':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: 122153
//    
//    Attempting to convert '1e03ff':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '91300.0e-2':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: 913
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim values() As String = { " 214309 ", "1,064,181", "(0)", "10241+", _
                                 " + 21499 ", " +21499 ", "122153.00", _
                                 "1e03ff", "91300.0e-2" }
      Dim whitespace As NumberStyles =  NumberStyles.AllowLeadingWhite Or NumberStyles.AllowTrailingWhite
      Dim styles() As NumberStyles = { NumberStyles.None, _
                                       whitespace, _
                                       NumberStyles.AllowLeadingSign Or NumberStyles.AllowTrailingSign Or whitespace, _
                                       NumberStyles.AllowThousands Or NumberStyles.AllowCurrencySymbol, _
                                       NumberStyles.AllowExponent Or NumberStyles.AllowDecimalPoint }

      ' Attempt to convert each number using each style combination.
      For Each value As String In values
         Console.WriteLine("Attempting to convert '{0}':", value)
         For Each style As NumberStyles In styles
            Try
               Dim number As ULong = UInt64.Parse(value, style)
               Console.WriteLine("   {0}: {1}", style, number)
            Catch e As FormatException
               Console.WriteLine("   {0}: Bad Format", style)
            Catch e As OverflowException
               Console.WriteLine("   {0}: Overflow", value)         
            End Try         
         Next
         Console.WriteLine()
      Next
   End Sub
End Module
' The example displays the following output:
'    Attempting to convert ' 214309 ':
'       None: Bad Format
'       AllowLeadingWhite, AllowTrailingWhite: 214309
'       Integer, AllowTrailingSign: 214309
'       AllowThousands, AllowCurrencySymbol: Bad Format
'       AllowDecimalPoint, AllowExponent: Bad Format
'    
'    Attempting to convert '1,064,181':
'       None: Bad Format
'       AllowLeadingWhite, AllowTrailingWhite: Bad Format
'       Integer, AllowTrailingSign: Bad Format
'       AllowThousands, AllowCurrencySymbol: 1064181
'       AllowDecimalPoint, AllowExponent: Bad Format
'    
'    Attempting to convert '(0)':
'       None: Bad Format
'       AllowLeadingWhite, AllowTrailingWhite: Bad Format
'       Integer, AllowTrailingSign: Bad Format
'       AllowThousands, AllowCurrencySymbol: Bad Format
'       AllowDecimalPoint, AllowExponent: Bad Format
'    
'    Attempting to convert '10241+':
'       None: Bad Format
'       AllowLeadingWhite, AllowTrailingWhite: Bad Format
'       Integer, AllowTrailingSign: 10241
'       AllowThousands, AllowCurrencySymbol: Bad Format
'       AllowDecimalPoint, AllowExponent: Bad Format
'    
'    Attempting to convert ' + 21499 ':
'       None: Bad Format
'       AllowLeadingWhite, AllowTrailingWhite: Bad Format
'       Integer, AllowTrailingSign: Bad Format
'       AllowThousands, AllowCurrencySymbol: Bad Format
'       AllowDecimalPoint, AllowExponent: Bad Format
'    
'    Attempting to convert ' +21499 ':
'       None: Bad Format
'       AllowLeadingWhite, AllowTrailingWhite: Bad Format
'       Integer, AllowTrailingSign: 21499
'       AllowThousands, AllowCurrencySymbol: Bad Format
'       AllowDecimalPoint, AllowExponent: Bad Format
'    
'    Attempting to convert '122153.00':
'       None: Bad Format
'       AllowLeadingWhite, AllowTrailingWhite: Bad Format
'       Integer, AllowTrailingSign: Bad Format
'       AllowThousands, AllowCurrencySymbol: Bad Format
'       AllowDecimalPoint, AllowExponent: 122153
'    
'    Attempting to convert '1e03ff':
'       None: Bad Format
'       AllowLeadingWhite, AllowTrailingWhite: Bad Format
'       Integer, AllowTrailingSign: Bad Format
'       AllowThousands, AllowCurrencySymbol: Bad Format
'       AllowDecimalPoint, AllowExponent: Bad Format
'    
'    Attempting to convert '91300.0e-2':
'       None: Bad Format
'       AllowLeadingWhite, AllowTrailingWhite: Bad Format
'       Integer, AllowTrailingSign: Bad Format
'       AllowThousands, AllowCurrencySymbol: Bad Format
'       AllowDecimalPoint, AllowExponent: 913

Uwagi

Parametr style definiuje elementy stylu (takie jak biały znak, symbol znaku dodatniego lub ujemnego, symbol separatora grupy lub symbol separatora dziesiętnego), które są dozwolone w parametrze s dla operacji analizy, aby zakończyć się powodzeniem. style musi być kombinacją flag bitowych z NumberStyles wyliczenia. Parametr style sprawia, że ta metoda jest przydatna, gdy s zawiera reprezentację ciągu wartości szesnastkowej, gdy system liczbowy (dziesiętny lub szesnastkowy) reprezentowany przez s element jest znany tylko w czasie wykonywania, lub gdy chcesz uniemożliwić biały znak lub symbol znaku w .s

W zależności od wartości styleparametr może s zawierać następujące elementy:

[ws][$][znak][cyfry,]cyfry[.fractional_digits][E[znak]exponential_digits][ws]

Elementy w nawiasach kwadratowych ([ i ]) są opcjonalne. Jeśli style parametr zawiera NumberStyles.AllowHexSpecifierparametr , s może zawierać następujące elementy:

[ws]hexdigits[ws]

W poniższej tabeli opisano każdy element.

Pierwiastek Opis
Ws Opcjonalne białe znaki. Białe znaki mogą pojawić się na początku s , jeśli style zawiera flagę NumberStyles.AllowLeadingWhite , i może pojawić się na końcu s , jeśli style zawiera flagę NumberStyles.AllowTrailingWhite .
$ Symbol waluty specyficznej dla kultury. Jego pozycja w ciągu jest definiowana przez NumberFormatInfo.CurrencyNegativePattern właściwości i NumberFormatInfo.CurrencyPositivePattern bieżącej kultury. Symbol waluty bieżącej kultury może pojawić się, s jeśli style zawiera flagę NumberStyles.AllowCurrencySymbol .
podpis Opcjonalny znak. Znak może pojawić się na początku s , jeśli style zawiera flagę NumberStyles.AllowLeadingSign i może pojawić się na końcu s , jeśli style zawiera flagę NumberStyles.AllowTrailingSign . Nawiasy mogą służyć do wskazywania wartości ujemnej, s jeśli style zawiera flagę NumberStyles.AllowParentheses . Jednak symbol znaku ujemnego może być używany tylko z zerem; w przeciwnym razie metoda zgłasza błąd OverflowException.
Cyfr

fractional_digits

exponential_digits
Sekwencja cyfr z zakresu od 0 do 9. W przypadku fractional_digits tylko cyfra 0 jest prawidłowa.
, Symbol separatora grup specyficznych dla kultury. Separator grupy bieżącej kultury może pojawić się, s jeśli style zawiera flagę NumberStyles.AllowThousands .
. Symbol separatora dziesiętnego specyficznego dla kultury. Symbol dziesiętny bieżącej kultury może pojawić się, s jeśli style zawiera flagę NumberStyles.AllowDecimalPoint . Tylko cyfra 0 może być wyświetlana jako cyfra ułamkowa dla operacji analizy, która powiedzie się; jeśli fractional_digits zawiera dowolną inną cyfrę, zostanie zgłoszony element FormatException .
E Znak "e" lub "E", który wskazuje, że wartość jest reprezentowana w notacji wykładniczej (naukowej). Parametr s może reprezentować liczbę w notacji wykładniczej, jeśli style zawiera flagę NumberStyles.AllowExponent .
hexdigits Sekwencja cyfr szesnastkowa od 0 do f lub od 0 do F.

Nuta

Wszystkie znaki NUL (U+0000) w obiekcie s są ignorowane przez operację analizowania, niezależnie od wartości argumentu style .

Ciąg z cyframi (który odpowiada NumberStyles.None stylowi) zawsze analizuje się pomyślnie, jeśli znajduje się w zakresie UInt64 typu. Większość pozostałych NumberStyles elementów członkowskich kontroluje elementy, które mogą być obecne, ale nie muszą być obecne, w ciągu wejściowym. W poniższej tabeli przedstawiono, jak poszczególne NumberStyles elementy członkowskie wpływają na elementy, które mogą być obecne w programie s.

NumberStyles wartość Elementy dozwolone s oprócz cyfr
None Tylko element cyfry .
AllowDecimalPoint Elementy separatora dziesiętnego (.) i cyfr ułamkowych .
AllowExponent Znak "e" lub "E", który wskazuje notację wykładniczą wraz z exponential_digits.
AllowLeadingWhite Element ws na początku .s
AllowTrailingWhite Element ws na końcu elementu s.
AllowLeadingSign Element sign na początku .s
AllowTrailingSign Element sign na końcu elementu s.
AllowParentheses Element znaku w postaci nawiasów ujętą w wartość liczbową.
AllowThousands Element separatora grup (,).
AllowCurrencySymbol Element currency ($).
Currency Wszystkie elementy. s Nie można jednak reprezentować liczby szesnastkowej ani liczby w notacji wykładniczej.
Float Element ws na początku lub na końcu sznaku na początku znaku i symbolu przecinka dziesiętnego s(.). Parametr s może również używać notacji wykładniczej.
Number wsElementy separatora signgrup (,) i separatora dziesiętnego (.).
Any Wszystkie elementy. s Nie można jednak reprezentować liczby szesnastkowej.

W przeciwieństwie do innych NumberStyles wartości, które umożliwiają, ale nie wymagają, obecność określonych elementów stylu w selemecie NumberStyles.AllowHexSpecifier oznacza, że poszczególne znaki liczbowe w s obiekcie są zawsze interpretowane jako znaki szesnastkowe. Prawidłowe znaki szesnastkowe to 0-9, A-F i a-f. Prefiks taki jak "0x" nie jest obsługiwany i powoduje niepowodzenie operacji analizowania. Jedynymi innymi flagami, które można połączyć z parametrem style , są NumberStyles.AllowLeadingWhite i NumberStyles.AllowTrailingWhite. (Wyliczenie NumberStyles zawiera styl liczby złożonej, NumberStyles.HexNumber, który zawiera obie flagi odstępu).

Nuta

Jeśli s jest reprezentacją ciągu liczby szesnastkowej, nie może być poprzedzona żadną dekoracją (taką jak 0x lub &h), która odróżnia ją jako liczbę szesnastkową. Powoduje to niepowodzenie konwersji.

Parametr s jest analizowany przy użyciu informacji o formatowaniu w obiekcie zainicjowanym NumberFormatInfo dla bieżącej kultury systemu. Aby określić kulturę, której informacje o formatowaniu są używane dla operacji analizowania, wywołaj Parse(String, NumberStyles, IFormatProvider) przeciążenie.

Zobacz też

Dotyczy