Udostępnij za pośrednictwem


UInt64.Parse Metoda

Definicja

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

Przeciążenia

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

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 przez parametr style.

style
NumberStyles

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

provider
IFormatProvider

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

Zwraca

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

Implementuje

Atrybuty

Wyjątki

Parametr s jest null.

style nie jest wartością NumberStyles.

-lub-

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

Parametr s nie jest w formacie zgodnym z style.

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

-lub-

s zawiera cyfry niezerowe, ułamkowe.

Przykłady

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

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, aby operacja analizy zakończyła się pomyślnie. Musi to być kombinacja flag bitowych z wyliczenia NumberStyles.

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

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

Elementy w nawiasach kwadratowych ([ i ]) są opcjonalne. Jeśli style zawiera NumberStyles.AllowHexSpecifier, parametr 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 właściwość CurrencyPositivePattern obiektu NumberFormatInfo, który jest zwracany przez metodę GetFormat parametru provider. Symbol waluty może pojawić się w s, jeśli style zawiera flagę NumberStyles.AllowCurrencySymbol.
podpisywania Opcjonalny znak. (Metoda zgłasza OverflowException, jeśli 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ą być używane w s, aby wskazać wartość ujemną, jeśli style zawiera flagę NumberStyles.AllowParentheses.
cyfry Sekwencja cyfr z zakresu od 0 do 9.
. Symbol separatora dziesiętnego specyficznego dla kultury. Symbol punktu dziesiętnego bieżącej kultury może pojawić się w 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 w s tylko wtedy, 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.
szesnastkowy Sekwencja cyfr szesnastkowa od 0 do f lub od 0 do F.

Nuta

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

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

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

Jeśli jest używana flaga NumberStyles.AllowHexSpecifier, 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 liczb złożonych, NumberStyles.HexNumber, który zawiera obie flagi odstępu).

Nuta

Jeśli parametr s 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 to implementacja IFormatProvider, której metoda GetFormat zwraca obiekt NumberFormatInfo, który udostępnia informacje specyficzne dla kultury dotyczące formatu s. Istnieją trzy sposoby użycia parametru provider do podawania niestandardowych informacji formatowania do operacji analizowania:

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

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

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

Jeśli provider jest null, używany jest obiekt NumberFormatInfo dla 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

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 przez parametr style.

style
NumberStyles

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

provider
IFormatProvider

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

Zwraca

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

Implementuje

Atrybuty

Dotyczy

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

Ź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 utf8Text.

provider
IFormatProvider

Obiekt, który udostępnia informacje o formatowaniu specyficznym dla kultury na temat 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

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 na temat s.

Zwraca

64-bitowa liczba całkowita bez znaku równoważna liczbie określonej w 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ększą niż UInt64.MaxValue.

Przykłady

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

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 metody Parse(String, IFormatProvider) jest zwykle używane do konwertowania tekstu, który można sformatować na różne sposoby na wartość UInt64. 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.
podpisywania Opcjonalny znak dodatni lub znak ujemny, jeśli s reprezentuje wartość zero.
cyfry Sekwencja cyfr od 0 do 9.

Parametr s jest interpretowany przy użyciu stylu NumberStyles.Integer. 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 OverflowException.) Aby jawnie zdefiniować elementy stylu wraz z informacjami o formatowaniu specyficznymi dla kultury, które mogą być obecne w s, użyj metody Parse(String, NumberStyles, IFormatProvider).

Parametr provider to implementacja IFormatProvider, której metoda GetFormat zwraca obiekt NumberFormatInfo, który udostępnia informacje specyficzne dla kultury dotyczące formatu s. Istnieją trzy sposoby użycia parametru provider do podawania niestandardowych informacji formatowania do operacji analizowania:

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

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

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

Jeśli provider jest null, zostanie użyta NumberFormatInfo 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

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 na temat s.

Zwraca

Wynik analizowania s.

Implementuje

Dotyczy

Parse(ReadOnlySpan<Byte>, IFormatProvider)

Ź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 na temat utf8Text.

Zwraca

Wynik analizowania utf8Text.

Implementuje

Dotyczy

Parse(String)

Ź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 s.

Atrybuty

Wyjątki

Parametr s jest null.

Parametr s nie ma poprawnego formatu.

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

Przykłady

W poniższym przykładzie użyto metody Parse, 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] [podpisywania]cyfry[ws]

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

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

Nuta

Ciąg określony przez parametr s jest interpretowany przy użyciu stylu NumberStyles.Integer. 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 System.Globalization.NumberFormatInfo zainicjowanym 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 metody Parse(String, IFormatProvider).

Zobacz też

Dotyczy

Parse(String, NumberStyles)

Ź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 przez parametr style.

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 s.

Atrybuty

Wyjątki

Parametr s jest null.

style nie jest wartością NumberStyles.

-lub-

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

Parametr s nie jest w formacie zgodnym z style.

Parametr s reprezentuje liczbę mniejszą niż UInt64.MinValue lub większą 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 przy użyciu kilku NumberStyles 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, która zakończy się powodzeniem. style musi być kombinacją flag bitowych z wyliczenia NumberStyles. Parametr style sprawia, że to przeciążenie metody jest przydatne, gdy s zawiera reprezentację ciągu wartości szesnastkowej, gdy system liczbowy (dziesiętny lub szesnastkowy) reprezentowany przez s 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 s może zawierać następujące elementy:

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

Elementy w nawiasach kwadratowych ([ i ]) są opcjonalne. Jeśli style zawiera NumberStyles.AllowHexSpecifier, parametr 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 właściwości NumberFormatInfo.CurrencyNegativePattern i NumberFormatInfo.CurrencyPositivePattern bieżącej kultury. Symbol waluty bieżącej kultury może pojawić się w s, jeśli style zawiera flagę NumberStyles.AllowCurrencySymbol.
podpisywania 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ą być używane w s, aby wskazać wartość ujemną, 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 OverflowException.
cyfry

fractional_digits

exponential_digits
Sekwencja cyfr z zakresu od 0 do 9. W przypadku fractional_digitstylko cyfra 0 jest prawidłowa.
, Symbol separatora grup specyficznych dla kultury. Separator grupy bieżącej kultury może pojawić się w s, jeśli style zawiera flagę NumberStyles.AllowThousands.
. Symbol separatora dziesiętnego specyficznego dla kultury. Symbol punktu dziesiętnego bieżącej kultury może pojawić się w 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 inną cyfrę, zostanie zgłoszony 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.
szesnastkowy Sekwencja cyfr szesnastkowa od 0 do f lub od 0 do F.

Nuta

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

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

NumberStyles wartość Elementy dozwolone w s oprócz cyfr
None Cyfry tylko element.
AllowDecimalPoint Liczba dziesiętnych (.) i cyfr ułamkowych elementów.
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 s.
AllowLeadingSign Element na początku s.
AllowTrailingSign Element na końcu s.
AllowParentheses Znak element w postaci nawiasów otaczających wartość liczbową.
AllowThousands Element separatora grup (,).
AllowCurrencySymbol Element currency ($).
Currency Wszystkie elementy. Jednak s nie może reprezentować liczby szesnastkowej ani liczby w notacji wykładniczej.
Float Element ws na początku lub na końcu s, znak na początku si symbol dziesiętny (.). Parametr s może również używać notacji wykładniczej.
Number Elementy ws, sign, separatora grup (,) i separatora dziesiętnego (.).
Any Wszystkie elementy. Jednak s nie może reprezentować liczby szesnastkowej.

W przeciwieństwie do innych wartości NumberStyles, które umożliwiają, ale nie wymagają, obecność określonych elementów stylu w swartość stylu NumberStyles.AllowHexSpecifier oznacza, że poszczególne znaki liczbowe w s 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 liczb złożonych, 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 NumberFormatInfo zainicjowanym dla bieżącej kultury systemu. Aby określić kulturę, której informacje o formatowaniu są używane dla operacji analizowania, wywołaj przeciążenie Parse(String, NumberStyles, IFormatProvider).

Zobacz też

Dotyczy