Int64.Parse Metoda
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Konwertuje reprezentację ciągu liczby na 64-bitową liczbę całkowitą ze znakiem.
Przeciążenia
| Nazwa | Opis |
|---|---|
| Parse(String) |
Konwertuje reprezentację ciągu liczby na 64-bitową liczbę całkowitą ze znakiem. |
| Parse(ReadOnlySpan<Byte>, IFormatProvider) |
Analizuje zakres znaków UTF-8 w wartość. |
| Parse(ReadOnlySpan<Char>, IFormatProvider) |
Analizuje zakres znaków w wartości. |
| Parse(String, NumberStyles) |
Konwertuje reprezentację ciągu liczby w określonym stylu na 64-bitową liczbę całkowitą ze znakiem. |
| Parse(String, IFormatProvider) |
Konwertuje reprezentację ciągu liczby w określonym formacie specyficznym dla kultury na 64-bitową liczbę całkowitą ze znakiem. |
| Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider) |
Analizuje zakres znaków UTF-8 w wartość. |
| Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) |
Konwertuje reprezentację zakresu liczby w określonym stylu i formacie specyficznym dla kultury na 64-bitową liczbę całkowitą ze znakiem. |
| Parse(String, NumberStyles, IFormatProvider) |
Konwertuje reprezentację ciągu liczby w określonym stylu i formacie specyficznym dla kultury na 64-bitową liczbę całkowitą ze znakiem. |
Parse(String)
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
Konwertuje reprezentację ciągu liczby na 64-bitową liczbę całkowitą ze znakiem.
public:
static long Parse(System::String ^ s);
public static long Parse(string s);
static member Parse : string -> int64
Public Shared Function Parse (s As String) As Long
Parametry
- s
- String
Ciąg zawierający liczbę do przekonwertowania.
Zwraca
64-bitowa liczba całkowita ze znakiem równoważna liczbie zawartej w elemencie s.
Wyjątki
Parametr s ma wartość null.
s nie jest w poprawnym formacie.
s reprezentuje liczbę mniejszą niż Int64.MinValue lub większą niż Int64.MaxValue.
Przykłady
W poniższym przykładzie pokazano, jak przekonwertować wartość ciągu na 64-bitową wartość całkowitą ze znakiem Int64.Parse(String) przy użyciu metody . Następnie zostanie wyświetlona wynikowa wartość długa liczba całkowita.
using System;
public class ParseInt64
{
public static void Main()
{
Convert(" 179042 ");
Convert(" -2041326 ");
Convert(" +8091522 ");
Convert(" 1064.0 ");
Convert(" 178.3");
Convert(String.Empty);
Convert(((decimal) Int64.MaxValue) + 1.ToString());
}
private static void Convert(string value)
{
try
{
long number = Int64.Parse(value);
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to convert '{0}'.", value);
}
catch (OverflowException)
{
Console.WriteLine("'{0}' is out of range.", value);
}
}
}
// This example displays the following output to the console:
// Converted ' 179042 ' to 179042.
// Converted ' -2041326 ' to -2041326.
// Converted ' +8091522 ' to 8091522.
// Unable to convert ' 1064.0 '.
// Unable to convert ' 178.3'.
// Unable to convert ''.
// '92233720368547758071' is out of range.
open System
let convert value =
try
let number = Int64.Parse value
printfn $"Converted '{value}' to {number}."
with
| :? FormatException ->
printfn $"Unable to convert '{value}'."
| :? OverflowException ->
printfn $"'{value}' is out of range."
convert " 179042 "
convert " -2041326 "
convert " +8091522 "
convert " 1064.0 "
convert " 178.3"
convert String.Empty
decimal Int64.MaxValue + 1M
|> string
|> convert
// This example displays the following output to the console:
// Converted ' 179042 ' to 179042.
// Converted ' -2041326 ' to -2041326.
// Converted ' +8091522 ' to 8091522.
// Unable to convert ' 1064.0 '.
// Unable to convert ' 178.3'.
// Unable to convert ''.
// '92233720368547758071' is out of range.
Module ParseInt64
Public Sub Main()
Convert(" 179032 ")
Convert(" -2041326 ")
Convert(" +8091522 ")
Convert(" 1064.0 ")
Convert(" 178.3")
Convert(String.Empty)
Convert((CDec(Int64.MaxValue) + 1).ToString())
End Sub
Private Sub Convert(value As String)
Try
Dim number As Long = Int64.Parse(value)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to convert '{0}'.", value)
Catch e As OverflowException
Console.WriteLine("'{0}' is out of range.", value)
End Try
End Sub
End Module
' This example displays the following output to the console:
' Converted ' 179032 ' to 179032.
' Converted ' -2041326 ' to -2041326.
' Converted ' +8091522 ' to 8091522.
' Unable to convert ' 1064.0 '.
' Unable to convert ' 178.3'.
' Unable to convert ''.
' '9223372036854775808' is out of range.
Uwagi
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. |
| znak | Opcjonalny znak. |
| Cyfr | Sekwencja cyfr od 0 do 9. |
Parametr s jest interpretowany przy użyciu NumberStyles.Integer stylu. Oprócz cyfr dziesiętnych dozwolone są tylko spacje wiodące i końcowe wraz z znakiem wiodącym. Aby jawnie zdefiniować elementy stylu, które mogą być obecne w sprogramie , użyj Int64.Parse(String, NumberStyles) metody lub Int64.Parse(String, NumberStyles, IFormatProvider) .
Parametr s jest analizowany przy użyciu informacji o formatowaniu w obiekcie zainicjowanym NumberFormatInfo dla bieżącej kultury systemu. Aby przeanalizować ciąg przy użyciu informacji o formatowaniu innej kultury, użyj Int64.Parse(String, NumberStyles, IFormatProvider) metody .
Zobacz też
Dotyczy
Parse(ReadOnlySpan<Byte>, IFormatProvider)
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
Analizuje zakres znaków UTF-8 w wartość.
public:
static long Parse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider) = IUtf8SpanParsable<long>::Parse;
public static long Parse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);
static member Parse : ReadOnlySpan<byte> * IFormatProvider -> int64
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider) As Long
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(ReadOnlySpan<Char>, IFormatProvider)
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
Analizuje zakres znaków w wartości.
public:
static long Parse(ReadOnlySpan<char> s, IFormatProvider ^ provider) = ISpanParsable<long>::Parse;
public static long Parse(ReadOnlySpan<char> s, IFormatProvider? provider);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> int64
Public Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As Long
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(String, NumberStyles)
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
Konwertuje reprezentację ciągu liczby w określonym stylu na 64-bitową liczbę całkowitą ze znakiem.
public:
static long Parse(System::String ^ s, System::Globalization::NumberStyles style);
public static long Parse(string s, System.Globalization.NumberStyles style);
static member Parse : string * System.Globalization.NumberStyles -> int64
Public Shared Function Parse (s As String, style As NumberStyles) As Long
Parametry
- s
- String
Ciąg zawierający liczbę do przekonwertowania.
- style
- NumberStyles
Bitowa kombinacja NumberStyles wartości wskazująca dozwolony format s. Typową wartością do określenia jest Integer.
Zwraca
64-bitowa liczba całkowita ze znakiem odpowiada liczbie określonej w elemencie s.
Wyjątki
Parametr s ma wartość null.
style nie jest wartością NumberStyles .
-lub-
style nie jest kombinacją AllowHexSpecifier wartości i HexNumber .
s nie jest w formacie zgodnym z style.
s reprezentuje liczbę mniejszą niż Int64.MinValue lub większą niż Int64.MaxValue.
-lub-
style obsługuje cyfry ułamkowe, ale s zawiera cyfry ułamkowe niezerowe.
Przykłady
W poniższym przykładzie użyto Int64.Parse(String, NumberStyles) metody , aby przeanalizować reprezentacje ciągu kilku Int64 wartości. Bieżąca kultura przykładu to en-US.
using System;
using System.Globalization;
public class ParseInt32
{
public static void Main()
{
Convert("104.0", NumberStyles.AllowDecimalPoint);
Convert("104.9", NumberStyles.AllowDecimalPoint);
Convert (" 106034", NumberStyles.None);
Convert(" $17,198,064.42", NumberStyles.AllowCurrencySymbol |
NumberStyles.Number);
Convert(" $17,198,064.00", NumberStyles.AllowCurrencySymbol |
NumberStyles.Number);
Convert("103E06", NumberStyles.AllowExponent);
Convert("1200E-02", NumberStyles.AllowExponent);
Convert("1200E-03", NumberStyles.AllowExponent);
Convert("-1,345,791", NumberStyles.AllowThousands);
Convert("(1,345,791)", NumberStyles.AllowThousands |
NumberStyles.AllowParentheses);
Convert("FFCA00A0", NumberStyles.HexNumber);
Convert("0xFFCA00A0", NumberStyles.HexNumber);
}
private static void Convert(string value, NumberStyles style)
{
try
{
long number = Int64.Parse(value, style);
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to convert '{0}'.", value);
}
catch (OverflowException)
{
Console.WriteLine("'{0}' is out of range of the Int64 type.", value);
}
}
}
// The example displays the following output to the console:
// Converted '104.0' to 104.
// '104.9' is out of range of the Int64 type.
// Unable to convert ' 106034'.
// ' $17,198,064.42' is out of range of the Int64 type.
// Converted ' $17,198,064.00' to 17198064.
// Converted '103E06' to 103000000.
// Converted '1200E-02' to 12.
// '1200E-03' is out of range of the Int64 type.
// Unable to convert '-1,345,791'.
// Converted '(1,345,791)' to -1345791.
// Converted 'FFCA00A0' to 4291428512.
// Unable to convert '0xFFCA00A0'.
open System
open System.Globalization
let convert value (style: NumberStyles) =
try
let number = Int64.Parse(value, style)
printfn $"converted '{value}' to {number}."
with
| :? FormatException ->
printfn $"Unable to convert '{value}'."
| :? OverflowException ->
printfn $"'{value}' is out of range of the Int64 type."
convert "104.0" NumberStyles.AllowDecimalPoint
convert "104.9" NumberStyles.AllowDecimalPoint
convert " 106034" NumberStyles.None
convert " $17,198,064.42" (NumberStyles.AllowCurrencySymbol ||| NumberStyles.Number)
convert " $17,198,064.00" (NumberStyles.AllowCurrencySymbol ||| NumberStyles.Number)
convert "103E06" NumberStyles.AllowExponent
convert "1200E-02" NumberStyles.AllowExponent
convert "1200E-03" NumberStyles.AllowExponent
convert "-1,345,791" NumberStyles.AllowThousands
convert "(1,345,791)" (NumberStyles.AllowThousands ||| NumberStyles.AllowParentheses)
convert "FFCA00A0" NumberStyles.HexNumber
convert "0xFFCA00A0" NumberStyles.HexNumber
// The example displays the following output to the console:
// converted '104.0' to 104.
// '104.9' is out of range of the Int64 type.
// Unable to convert ' 106034'.
// ' $17,198,064.42' is out of range of the Int64 type.
// converted ' $17,198,064.00' to 17198064.
// converted '103E06' to 103000000.
// converted '1200E-02' to 12.
// '1200E-03' is out of range of the Int64 type.
// Unable to convert '-1,345,791'.
// converted '(1,345,791)' to -1345791.
// converted 'FFCA00A0' to 4291428512.
// Unable to convert '0xFFCA00A0'.
Imports System.Globalization
Module ParseInt64
Public Sub Main()
Convert("104.0", NumberStyles.AllowDecimalPoint)
Convert("104.9", NumberStyles.AllowDecimalPoint)
Convert (" 106034", NumberStyles.None)
Convert(" $17,198,064.42", NumberStyles.AllowCurrencySymbol Or _
NumberStyles.Number)
Convert(" $17,198,064.00", NumberStyles.AllowCurrencySymbol Or _
NumberStyles.Number)
Convert("103E06", NumberStyles.AllowExponent)
Convert("1200E-02", NumberStyles.AllowExponent)
Convert("1200E-03", NumberStyles.AllowExponent)
Convert("-1,345,791", NumberStyles.AllowThousands)
Convert("(1,345,791)", NumberStyles.AllowThousands Or _
NumberStyles.AllowParentheses)
Convert("FFCA00A0", NumberStyles.HexNumber)
Convert("0xFFCA00A0", NumberStyles.HexNumber)
End Sub
Private Sub Convert(value As String, style As NumberStyles)
Try
Dim number As Long = Int64.Parse(value, style)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to convert '{0}'.", value)
Catch e As OverflowException
Console.WriteLine("'{0}' is out of range of the Int64 type.", value)
End Try
End Sub
End Module
' The example displays the following output to the console:
' Converted '104.0' to 104.
' '104.9' is out of range of the Int64 type.
' Unable to convert ' 106034'.
' ' $17,198,064.42' is out of range of the Int64 type.
' Converted ' $17,198,064.00' to 17198064.
' Converted '103E06' to 103000000.
' Converted '1200E-02' to 12.
' '1200E-03' is out of range of the Int64 type.
' Unable to convert '-1,345,791'.
' Converted '(1,345,791)' to -1345791.
' Converted 'FFCA00A0' to 4291428512.
' Unable to convert '0xFFCA00A0'.
Uwagi
Parametr style definiuje elementy stylu (takie jak biały znak, symbol znaku dodatniego lub ujemnego lub symbol separatora tysięcy), które są dozwolone w parametrze s dla operacji analizy, która zakończy się powodzeniem. 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] [$] [podpis] [cyfry,]cyfry[.fractional_digits][e[znak]exponential_digits][ws]
Lub, jeśli style obejmuje AllowHexSpecifier:
[ws]hexdigits[ws]
Elementy w nawiasach kwadratowych ([ i ]) są opcjonalne. 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 . |
|
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 tysięcy specyficzny dla kultury. Separator tysięcy 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.OverflowException |
| e | Znak "e" lub "E", który wskazuje, że wartość jest reprezentowana w notacji wykładniczej. 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 Int64 typu. Większość pozostałych NumberStyles elementów członkowskich kontroluje elementy, które mogą być, 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.
| Wartość NumberStyles | Elementy dozwolone w s oprócz cyfr |
|---|---|
| None | Tylko element cyfry . |
| AllowDecimalPoint | Elementy punktów dziesiętnych ( . ) i cyfr ułamkowych . |
| AllowExponent | Parametr s może również używać notacji wykładniczej. Jeśli s reprezentuje liczbę w notacji wykładniczej, wynikowa wartość liczbowa nie może zawierać żadnych cyfr ułamkowych innych niż zero. |
| AllowLeadingWhite | Element ws na początku elementu s. |
| AllowTrailingWhite | Element ws na końcu elementu s. |
| AllowLeadingSign | Element sign na początku elementu s. |
| AllowTrailingSign | Element sign na końcu elementu s. |
| AllowParentheses | Element znaku w postaci nawiasów ujętą w wartość liczbową. |
| AllowThousands | Separator tysięcy ( , ) , element. |
| AllowCurrencySymbol | Element $ . |
| Currency | Cały. Parametr s nie może reprezentować liczby szesnastkowej ani liczby w notacji wykładniczej. |
| Float | Element ws na początku lub na końcu sznaku na początku s, i symbol dziesiętny ( . ). Parametr s może również używać notacji wykładniczej. |
| Number | Elementy ws, znak, separator tysięcy ( , ) i separator dziesiętny ( . ). |
| Any | Wszystkie style, z wyjątkiem s nie mogą reprezentować liczby szesnastkowej. |
Jeśli flaga NumberStyles.AllowHexSpecifier jest używana, s musi być wartością szesnastkową bez prefiksu. Na przykład "C9AF3" analizuje się pomyślnie, ale "0xC9AF3" nie. Jedynymi innymi flagami, które można połączyć z parametrem s , są NumberStyles.AllowLeadingWhite i NumberStyles.AllowTrailingWhite. (Wyliczenie NumberStyles zawiera styl liczby złożonej, NumberStyles.HexNumber, który zawiera obie flagi odstępu).
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 Int64.Parse(String, NumberStyles, IFormatProvider) przeciążenie.
Zobacz też
Dotyczy
Parse(String, IFormatProvider)
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
Konwertuje reprezentację ciągu liczby w określonym formacie specyficznym dla kultury na 64-bitową liczbę całkowitą ze znakiem.
public:
static long Parse(System::String ^ s, IFormatProvider ^ provider);
public:
static long Parse(System::String ^ s, IFormatProvider ^ provider) = IParsable<long>::Parse;
public static long Parse(string s, IFormatProvider provider);
public static long Parse(string s, IFormatProvider? provider);
static member Parse : string * IFormatProvider -> int64
Public Shared Function Parse (s As String, provider As IFormatProvider) As Long
Parametry
- s
- String
Ciąg zawierający liczbę do przekonwertowania.
- provider
- IFormatProvider
Obiekt, który dostarcza informacje o formatowaniu specyficznym dla kultury.s
Zwraca
64-bitowa liczba całkowita ze znakiem odpowiada liczbie określonej w elemencie s.
Implementuje
Wyjątki
Parametr s ma wartość null.
s nie jest w poprawnym formacie.
s reprezentuje liczbę mniejszą niż Int64.MinValue lub większą niż Int64.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 Int64 wartość.
protected void OkToLong_Click(object sender, EventArgs e)
{
string locale;
long 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 = Int64.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 OkToLong_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OkToLong.Click
Dim locale As String
Dim culture As CultureInfo
Dim number As Long
' 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 = Int64.Parse(Me.inputNumber.Text, culture.NumberFormat)
Catch ex As FormatException
Exit Sub
Catch ex As Exception
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 Int64 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, a inne elementy są następujące.
ws Opcjonalne białe znaki.
podpisz opcjonalny znak.
digits Sekwencja cyfr od 0 do 9.
Parametr s jest interpretowany przy użyciu NumberStyles.Integer stylu. Oprócz cyfr dziesiętnych dozwolone są tylko spacje wiodące i końcowe wraz z znakiem wiodącym. Aby jawnie zdefiniować elementy stylu, które mogą być obecne w sprogramie , użyj Int64.Parse(String, NumberStyles, IFormatProvider) metody .
Parametr provider jest implementacją IFormatProvider , taką jak NumberFormatInfo obiekt lub CultureInfo . Parametr provider dostarcza informacje specyficzne dla kultury dotyczące formatu s. Jeśli provider wartość to null, NumberFormatInfo używana jest wartość dla bieżącej kultury.
Zobacz też
Dotyczy
Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
Analizuje zakres znaków UTF-8 w wartość.
public static long Parse(ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider -> int64
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As Long
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(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
Konwertuje reprezentację zakresu liczby w określonym stylu i formacie specyficznym dla kultury na 64-bitową liczbę całkowitą ze znakiem.
public static long Parse(ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
public static long Parse(ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider provider = default);
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> int64
Public Shared Function Parse (s As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As Long
Parametry
- s
- ReadOnlySpan<Char>
Zakres zawierający znaki reprezentujące liczbę do przekonwertowania.
- 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
Element IFormatProvider , który dostarcza informacje dotyczące formatowania specyficznego dla kultury na temat selementu .
Zwraca
64-bitowa liczba całkowita ze znakiem odpowiada liczbie określonej w elemencie s.
Implementuje
Dotyczy
Parse(String, NumberStyles, IFormatProvider)
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
- Źródło:
- Int64.cs
Konwertuje reprezentację ciągu liczby w określonym stylu i formacie specyficznym dla kultury na 64-bitową liczbę całkowitą ze znakiem.
public:
static long Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
public:
static long Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider) = System::Numerics::INumberBase<long>::Parse;
public static long Parse(string s, System.Globalization.NumberStyles style, IFormatProvider provider);
public static long Parse(string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> int64
Public Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As Long
Parametry
- s
- String
Ciąg zawierający liczbę do przekonwertowania.
- 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
Element IFormatProvider , który dostarcza informacje dotyczące formatowania specyficznego dla kultury na temat selementu .
Zwraca
64-bitowa liczba całkowita ze znakiem odpowiada liczbie określonej w elemencie s.
Implementuje
Wyjątki
Parametr s ma wartość null.
style nie jest wartością NumberStyles .
-lub-
style nie jest kombinacją AllowHexSpecifier wartości i HexNumber .
s nie jest w formacie zgodnym z style.
s reprezentuje liczbę mniejszą niż Int64.MinValue lub większą niż Int64.MaxValue.
-lub-
style obsługuje cyfry ułamkowe, ale s zawiera cyfry ułamkowe niezerowe.
Przykłady
W poniższym przykładzie użyto różnych parametrów style i provider , aby przeanalizować reprezentacje ciągów Int64 wartości. Ilustruje również niektóre różne sposoby interpretowania tego samego ciągu w zależności od kultury, której informacje formatowania są używane do operacji analizowania.
using System;
using System.Globalization;
public class ParseInt64
{
public static void Main()
{
Convert("12,000", NumberStyles.Float | NumberStyles.AllowThousands,
new CultureInfo("en-GB"));
Convert("12,000", NumberStyles.Float | NumberStyles.AllowThousands,
new CultureInfo("fr-FR"));
Convert("12,000", NumberStyles.Float, new CultureInfo("en-US"));
Convert("12 425,00", NumberStyles.Float | NumberStyles.AllowThousands,
new CultureInfo("sv-SE"));
Convert("12,425.00", NumberStyles.Float | NumberStyles.AllowThousands,
NumberFormatInfo.InvariantInfo);
Convert("631,900", NumberStyles.Integer | NumberStyles.AllowDecimalPoint,
new CultureInfo("fr-FR"));
Convert("631,900", NumberStyles.Integer | NumberStyles.AllowDecimalPoint,
new CultureInfo("en-US"));
Convert("631,900", NumberStyles.Integer | NumberStyles.AllowThousands,
new CultureInfo("en-US"));
}
private static void Convert(string value, NumberStyles style,
IFormatProvider provider)
{
try
{
long number = Int64.Parse(value, style, provider);
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to convert '{0}'.", value);
}
catch (OverflowException)
{
Console.WriteLine("'{0}' is out of range of the Int64 type.", value);
}
}
}
// This example displays the following output to the console:
// Converted '12,000' to 12000.
// Converted '12,000' to 12.
// Unable to convert '12,000'.
// Converted '12 425,00' to 12425.
// Converted '12,425.00' to 12425.
// '631,900' is out of range of the Int64 type.
// Unable to convert '631,900'.
// Converted '631,900' to 631900.
open System
open System.Globalization
let convert (value: string) style provider =
try
let number = Int64.Parse(value, style, provider)
printfn $"Converted '{value}' to {number}."
with
| :? FormatException ->
printfn $"Unable to convert '{value}'."
| :? OverflowException ->
printfn $"'{value}' is out of range of the Int64 type."
convert "12,000" (NumberStyles.Float ||| NumberStyles.AllowThousands) (CultureInfo "en-GB")
convert "12,000" (NumberStyles.Float ||| NumberStyles.AllowThousands) (CultureInfo "fr-FR")
convert "12,000" NumberStyles.Float (CultureInfo "en-US")
convert "12 425,00" (NumberStyles.Float ||| NumberStyles.AllowThousands) (CultureInfo "sv-SE")
convert "12,425.00" (NumberStyles.Float ||| NumberStyles.AllowThousands) NumberFormatInfo.InvariantInfo
convert "631,900" (NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint) (CultureInfo "fr-FR")
convert "631,900" (NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint) (CultureInfo "en-US")
convert "631,900" (NumberStyles.Integer ||| NumberStyles.AllowThousands) (CultureInfo "en-US")
// This example displays the following output to the console:
// Converted '12,000' to 12000.
// Converted '12,000' to 12.
// Unable to convert '12,000'.
// Converted '12 425,00' to 12425.
// Converted '12,425.00' to 12425.
// '631,900' is out of range of the Int64 type.
// Unable to convert '631,900'.
// Converted '631,900' to 631900.
Imports System.Globalization
Module ParseInt64
Public Sub Main()
Convert("12,000", NumberStyles.Float Or NumberStyles.AllowThousands, _
New CultureInfo("en-GB"))
Convert("12,000", NumberStyles.Float Or NumberStyles.AllowThousands, _
New CultureInfo("fr-FR"))
Convert("12,000", NumberStyles.Float, New CultureInfo("en-US"))
Convert("12 425,00", NumberStyles.Float Or NumberStyles.AllowThousands, _
New CultureInfo("sv-SE"))
Convert("12,425.00", NumberStyles.Float Or NumberStyles.AllowThousands, _
NumberFormatInfo.InvariantInfo)
Convert("631,900", NumberStyles.Integer Or NumberStyles.AllowDecimalPoint, _
New CultureInfo("fr-FR"))
Convert("631,900", NumberStyles.Integer Or NumberStyles.AllowDecimalPoint, _
New CultureInfo("en-US"))
Convert("631,900", NumberStyles.Integer Or NumberStyles.AllowThousands, _
New CultureInfo("en-US"))
End Sub
Private Sub Convert(value As String, style As NumberStyles, _
provider As IFormatProvider)
Try
Dim number As Long = Int64.Parse(value, style, provider)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to convert '{0}'.", value)
Catch e As OverflowException
Console.WriteLine("'{0}' is out of range of the Int64 type.", value)
End Try
End Sub
End Module
' This example displays the following output to the console:
' Converted '12,000' to 12000.
' Converted '12,000' to 12.
' Unable to convert '12,000'.
' Converted '12 425,00' to 12425.
' Converted '12,425.00' to 12425.
' '631,900' is out of range of the Int64 type.
' Unable to convert '631,900'.
' Converted '631,900' to 631900.
Uwagi
Parametr style definiuje elementy stylu (takie jak biały znak lub znak dodatni), które są dozwolone w parametrze s , aby operacja analizy zakończyła się powodzeniem. 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] [$] [podpis] [cyfry,]cyfry[.fractional_digits][e[znak]exponential_digits][ws]
Lub, jeśli style obejmuje AllowHexSpecifier:
[ws]hexdigits[ws]
Elementy w nawiasach kwadratowych ([ i ]) są opcjonalne. 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.CurrencyPositivePattern właściwość NumberFormatInfo obiektu zwróconego przez GetFormat metodę parametru provider . Symbol waluty 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 lub 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 fractional_digits exponential_digits |
Sekwencja cyfr z zakresu od 0 do 9. |
| , | Symbol separatora tysięcy specyficzny dla kultury. Separator tysięcy kultury określonej przez provider program może pojawić się, s jeśli style zawiera flagę NumberStyles.AllowThousands . |
| . | Symbol separatora dziesiętnego specyficznego dla kultury. Symbol separatora dziesiętnego kultury określonej przez provider element 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.OverflowException |
| e | Znak "e" lub "E", który wskazuje, że wartość jest reprezentowana w notacji wykładniczej. 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, jeśli znajduje się w zakresie Int64 typu. Większość pozostałych NumberStyles elementów członkowskich kontroluje elementy, które mogą być, 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.
| Nieskładne wartości NumberStyles | Elementy dozwolone w s oprócz cyfr |
|---|---|
| NumberStyles.None | Tylko cyfry dziesiętne. |
| NumberStyles.AllowDecimalPoint | Elementy punktów dziesiętnych ( . ) i cyfr ułamkowych . Jednak cyfry ułamkowe muszą składać się tylko z co najmniej jednej cyfry lub OverflowException jest zgłaszana. |
| NumberStyles.AllowExponent | Parametr s może również używać notacji wykładniczej. |
| NumberStyles.AllowLeadingWhite | Element ws na początku elementu s. |
| NumberStyles.AllowTrailingWhite | Element ws na końcu elementu s. |
| NumberStyles.AllowLeadingSign | Znak może pojawić się przed cyframi. |
| NumberStyles.AllowTrailingSign | Znak może pojawić się po cyfrach. |
| NumberStyles.AllowParentheses | Element znaku w postaci nawiasów ujętą w wartość liczbową. |
| NumberStyles.AllowThousands | Separator tysięcy ( , ) , element. |
| NumberStyles.AllowCurrencySymbol | Element $ . |
Jeśli flaga NumberStyles.AllowHexSpecifier jest używana, s musi być wartością szesnastkową bez prefiksu. Na przykład "C9AF3" analizuje się pomyślnie, ale "0xC9AF3" nie. Jedynymi innymi flagami, które mogą być obecne w systemie style , są NumberStyles.AllowLeadingWhite i NumberStyles.AllowTrailingWhite. (Wyliczenie NumberStyles ma styl liczby złożonej, NumberStyles.HexNumber, który zawiera obie flagi odstępu).
Parametr provider jest implementacją IFormatProvider , taką jak NumberFormatInfo obiekt lub CultureInfo . Parametr provider dostarcza informacje specyficzne dla kultury używane w analizowaniu. Jeśli provider wartość to null, NumberFormatInfo używana jest wartość dla bieżącej kultury.