CharUnicodeInfo.GetNumericValue 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得與 Unicode 字元關聯的數值。
多載
GetNumericValue(Char) |
取得與指定字元關聯的數值。 |
GetNumericValue(String, Int32) |
取得數值,該值與指定字串之指定索引處的字元關聯。 |
GetNumericValue(Char)
取得與指定字元關聯的數值。
public:
static double GetNumericValue(char ch);
public static double GetNumericValue (char ch);
static member GetNumericValue : char -> double
Public Shared Function GetNumericValue (ch As Char) As Double
參數
- ch
- Char
要取得數值的 Unicode 字元。
傳回
與指定字元關聯的數值。
-或-
如果指定的字元不是數字字元,則為 -1。
範例
下列程式代碼範例顯示每個方法針對不同類型的字元所傳回的值。
using namespace System;
using namespace System::Globalization;
void PrintProperties( Char c );
int main()
{
Console::WriteLine( " c Num Dig Dec UnicodeCategory" );
Console::Write( "U+0061 LATIN SMALL LETTER A " );
PrintProperties( L'a' );
Console::Write( "U+0393 GREEK CAPITAL LETTER GAMMA " );
PrintProperties( L'\u0393' );
Console::Write( "U+0039 DIGIT NINE " );
PrintProperties( L'9' );
Console::Write( "U+00B2 SUPERSCRIPT TWO " );
PrintProperties( L'\u00B2' );
Console::Write( "U+00BC VULGAR FRACTION ONE QUARTER " );
PrintProperties( L'\u00BC' );
Console::Write( "U+0BEF TAMIL DIGIT NINE " );
PrintProperties( L'\u0BEF' );
Console::Write( "U+0BF0 TAMIL NUMBER TEN " );
PrintProperties( L'\u0BF0' );
Console::Write( "U+0F33 TIBETAN DIGIT HALF ZERO " );
PrintProperties( L'\u0F33' );
Console::Write( "U+2788 CIRCLED SANS-SERIF DIGIT NINE " );
PrintProperties( L'\u2788' );
}
void PrintProperties( Char c )
{
Console::Write( " {0,-3}", c );
Console::Write( " {0,-5}", CharUnicodeInfo::GetNumericValue( c ) );
Console::Write( " {0,-5}", CharUnicodeInfo::GetDigitValue( c ) );
Console::Write( " {0,-5}", CharUnicodeInfo::GetDecimalDigitValue( c ) );
Console::WriteLine( "{0}", CharUnicodeInfo::GetUnicodeCategory( c ) );
}
/*
This code produces the following output. Some characters might not display at the console.
c Num Dig Dec UnicodeCategory
U+0061 LATIN SMALL LETTER A a -1 -1 -1 LowercaseLetter
U+0393 GREEK CAPITAL LETTER GAMMA Γ -1 -1 -1 UppercaseLetter
U+0039 DIGIT NINE 9 9 9 9 DecimalDigitNumber
U+00B2 SUPERSCRIPT TWO ² 2 2 -1 OtherNumber
U+00BC VULGAR FRACTION ONE QUARTER ¼ 0.25 -1 -1 OtherNumber
U+0BEF TAMIL DIGIT NINE ௯ 9 9 9 DecimalDigitNumber
U+0BF0 TAMIL NUMBER TEN ௰ 10 -1 -1 OtherNumber
U+0F33 TIBETAN DIGIT HALF ZERO ༳ -0.5 -1 -1 OtherNumber
U+2788 CIRCLED SANS-SERIF DIGIT NINE ➈ 9 9 -1 OtherNumber
*/
using System;
using System.Globalization;
public class SamplesCharUnicodeInfo {
public static void Main() {
Console.WriteLine( " c Num Dig Dec UnicodeCategory" );
Console.Write( "U+0061 LATIN SMALL LETTER A " );
PrintProperties( 'a' );
Console.Write( "U+0393 GREEK CAPITAL LETTER GAMMA " );
PrintProperties( '\u0393' );
Console.Write( "U+0039 DIGIT NINE " );
PrintProperties( '9' );
Console.Write( "U+00B2 SUPERSCRIPT TWO " );
PrintProperties( '\u00B2' );
Console.Write( "U+00BC VULGAR FRACTION ONE QUARTER " );
PrintProperties( '\u00BC' );
Console.Write( "U+0BEF TAMIL DIGIT NINE " );
PrintProperties( '\u0BEF' );
Console.Write( "U+0BF0 TAMIL NUMBER TEN " );
PrintProperties( '\u0BF0' );
Console.Write( "U+0F33 TIBETAN DIGIT HALF ZERO " );
PrintProperties( '\u0F33' );
Console.Write( "U+2788 CIRCLED SANS-SERIF DIGIT NINE " );
PrintProperties( '\u2788' );
}
public static void PrintProperties( char c ) {
Console.Write( " {0,-3}", c );
Console.Write( " {0,-5}", CharUnicodeInfo.GetNumericValue( c ) );
Console.Write( " {0,-5}", CharUnicodeInfo.GetDigitValue( c ) );
Console.Write( " {0,-5}", CharUnicodeInfo.GetDecimalDigitValue( c ) );
Console.WriteLine( "{0}", CharUnicodeInfo.GetUnicodeCategory( c ) );
}
}
/*
This code produces the following output. Some characters might not display at the console.
c Num Dig Dec UnicodeCategory
U+0061 LATIN SMALL LETTER A a -1 -1 -1 LowercaseLetter
U+0393 GREEK CAPITAL LETTER GAMMA Γ -1 -1 -1 UppercaseLetter
U+0039 DIGIT NINE 9 9 9 9 DecimalDigitNumber
U+00B2 SUPERSCRIPT TWO ² 2 2 -1 OtherNumber
U+00BC VULGAR FRACTION ONE QUARTER ¼ 0.25 -1 -1 OtherNumber
U+0BEF TAMIL DIGIT NINE ௯ 9 9 9 DecimalDigitNumber
U+0BF0 TAMIL NUMBER TEN ௰ 10 -1 -1 OtherNumber
U+0F33 TIBETAN DIGIT HALF ZERO ༳ -0.5 -1 -1 OtherNumber
U+2788 CIRCLED SANS-SERIF DIGIT NINE ➈ 9 9 -1 OtherNumber
*/
Imports System.Globalization
Public Class SamplesCharUnicodeInfo
Public Shared Sub Main()
Console.WriteLine(" c Num Dig Dec UnicodeCategory")
Console.Write("U+0061 LATIN SMALL LETTER A ")
PrintProperties("a"c)
Console.Write("U+0393 GREEK CAPITAL LETTER GAMMA ")
PrintProperties(ChrW(&H0393))
Console.Write("U+0039 DIGIT NINE ")
PrintProperties("9"c)
Console.Write("U+00B2 SUPERSCRIPT TWO ")
PrintProperties(ChrW(&H00B2))
Console.Write("U+00BC VULGAR FRACTION ONE QUARTER ")
PrintProperties(ChrW(&H00BC))
Console.Write("U+0BEF TAMIL DIGIT NINE ")
PrintProperties(ChrW(&H0BEF))
Console.Write("U+0BF0 TAMIL NUMBER TEN ")
PrintProperties(ChrW(&H0BF0))
Console.Write("U+0F33 TIBETAN DIGIT HALF ZERO ")
PrintProperties(ChrW(&H0F33))
Console.Write("U+2788 CIRCLED SANS-SERIF DIGIT NINE ")
PrintProperties(ChrW(&H2788))
End Sub
Public Shared Sub PrintProperties(c As Char)
Console.Write(" {0,-3}", c)
Console.Write(" {0,-5}", CharUnicodeInfo.GetNumericValue(c))
Console.Write(" {0,-5}", CharUnicodeInfo.GetDigitValue(c))
Console.Write(" {0,-5}", CharUnicodeInfo.GetDecimalDigitValue(c))
Console.WriteLine("{0}", CharUnicodeInfo.GetUnicodeCategory(c))
End Sub
End Class
'This code produces the following output. Some characters might not display at the console.
'
' c Num Dig Dec UnicodeCategory
'U+0061 LATIN SMALL LETTER A a -1 -1 -1 LowercaseLetter
'U+0393 GREEK CAPITAL LETTER GAMMA Γ -1 -1 -1 UppercaseLetter
'U+0039 DIGIT NINE 9 9 9 9 DecimalDigitNumber
'U+00B2 SUPERSCRIPT TWO ² 2 2 -1 OtherNumber
'U+00BC VULGAR FRACTION ONE QUARTER ¼ 0.25 -1 -1 OtherNumber
'U+0BEF TAMIL DIGIT NINE ௯ 9 9 9 DecimalDigitNumber
'U+0BF0 TAMIL NUMBER TEN ௰ 10 -1 -1 OtherNumber
'U+0F33 TIBETAN DIGIT HALF ZERO ༳ -0.5 -1 -1 OtherNumber
'U+2788 CIRCLED SANS-SERIF DIGIT NINE ➈ 9 9 -1 OtherNumber
備註
數值是 Unicode 字元屬性,僅適用於數值字元,其中包括分數、下標、上標、羅馬數位、貨幣 Numerator、列舉數位和腳本特定的數位。 如需 Unicode 字元的詳細資訊,請參閱 Unicode Standard。
方法 GetNumericValue(Char) 假設 ch
對應至單一語言字元,並檢查該字元是否可以轉換成十進位數。 不過,Unicode 標準中的某些數位是由構成 Surrogate 字組的兩 Char 個物件來表示。 例如,「啟用編號」系統是由代碼點 U+10107 到 U+10133 所組成。 下列範例會 ConvertFromUtf32 使用 方法來具現化代表一個 HYPERLINKN NUMBER ONE 的字串。 如範例的輸出所示,如果方法傳遞高 Surrogate 或這個字元的低 Surrogate,此方法 GetNumericValue(Char) 會傳回 -1。
int utf32 = 0x10107; // AEGEAN NUMBER ONE
string surrogate = Char.ConvertFromUtf32(utf32);
foreach (var ch in surrogate)
Console.WriteLine($"U+{(ushort)ch:X4}: {System.Globalization.CharUnicodeInfo.GetNumericValue(ch)} ");
// The example displays the following output:
// U+D800: -1
// U+DD07: -1
Dim utf32 As Integer = &h10107 ' AEGEAN NUMBER ONE
Dim surrogate As String = Char.ConvertFromUtf32(utf32)
For Each ch In surrogate
Console.WriteLine("U+{0:X4}: {1} ", Convert.ToUInt16(ch),
System.Globalization.CharUnicodeInfo.GetNumericValue(ch))
Next
' The example displays the following output:
' U+D800: -1
' U+DD07: -1
適用於
GetNumericValue(String, Int32)
取得數值,該值與指定字串之指定索引處的字元關聯。
public:
static double GetNumericValue(System::String ^ s, int index);
public static double GetNumericValue (string s, int index);
static member GetNumericValue : string * int -> double
Public Shared Function GetNumericValue (s As String, index As Integer) As Double
參數
- index
- Int32
要取得數值之 Unicode 字元的索引。
傳回
數值,該值與指定字串之指定索引處的字元關聯。
-或-
如果位於指定字串之指定索引處的字元不是數字字元,則為 -1。
例外狀況
s
為 null
。
index
在 s
的有效索引範圍之外。
範例
下列程式代碼範例顯示每個方法針對不同類型的字元所傳回的值。
using namespace System;
using namespace System::Globalization;
int main()
{
// The String to get information for.
String^ s = "a9\u0393\u00B2\u00BC\u0BEF\u0BF0\u2788";
Console::WriteLine( "String: {0}", s );
// Print the values for each of the characters in the string.
Console::WriteLine( "index c Num Dig Dec UnicodeCategory" );
for ( int i = 0; i < s->Length; i++ )
{
Console::Write( "{0,-5} {1,-3}", i, s[ i ] );
Console::Write( " {0,-5}", CharUnicodeInfo::GetNumericValue( s, i ) );
Console::Write( " {0,-5}", CharUnicodeInfo::GetDigitValue( s, i ) );
Console::Write( " {0,-5}", CharUnicodeInfo::GetDecimalDigitValue( s, i ) );
Console::WriteLine( "{0}", CharUnicodeInfo::GetUnicodeCategory( s, i ) );
}
}
/*
This code produces the following output. Some characters might not display at the console.
String: a9Γ²¼௯௰➈
index c Num Dig Dec UnicodeCategory
0 a -1 -1 -1 LowercaseLetter
1 9 9 9 9 DecimalDigitNumber
2 Γ -1 -1 -1 UppercaseLetter
3 ² 2 2 -1 OtherNumber
4 ¼ 0.25 -1 -1 OtherNumber
5 ௯ 9 9 9 DecimalDigitNumber
6 ௰ 10 -1 -1 OtherNumber
7 ➈ 9 9 -1 OtherNumber
*/
using System;
using System.Globalization;
public class SamplesCharUnicodeInfo {
public static void Main() {
// The String to get information for.
String s = "a9\u0393\u00B2\u00BC\u0BEF\u0BF0\u2788";
Console.WriteLine( "String: {0}", s );
// Print the values for each of the characters in the string.
Console.WriteLine( "index c Num Dig Dec UnicodeCategory" );
for ( int i = 0; i < s.Length; i++ ) {
Console.Write( "{0,-5} {1,-3}", i, s[i] );
Console.Write( " {0,-5}", CharUnicodeInfo.GetNumericValue( s, i ) );
Console.Write( " {0,-5}", CharUnicodeInfo.GetDigitValue( s, i ) );
Console.Write( " {0,-5}", CharUnicodeInfo.GetDecimalDigitValue( s, i ) );
Console.WriteLine( "{0}", CharUnicodeInfo.GetUnicodeCategory( s, i ) );
}
}
}
/*
This code produces the following output. Some characters might not display at the console.
String: a9Γ²¼௯௰➈
index c Num Dig Dec UnicodeCategory
0 a -1 -1 -1 LowercaseLetter
1 9 9 9 9 DecimalDigitNumber
2 Γ -1 -1 -1 UppercaseLetter
3 ² 2 2 -1 OtherNumber
4 ¼ 0.25 -1 -1 OtherNumber
5 ௯ 9 9 9 DecimalDigitNumber
6 ௰ 10 -1 -1 OtherNumber
7 ➈ 9 9 -1 OtherNumber
*/
Imports System.Globalization
Public Class SamplesCharUnicodeInfo
Public Shared Sub Main()
' The String to get information for.
Dim s As [String] = "a9\u0393\u00B2\u00BC\u0BEF\u0BF0\u2788"
Console.WriteLine("String: {0}", s)
' Print the values for each of the characters in the string.
Console.WriteLine("index c Num Dig Dec UnicodeCategory")
Dim i As Integer
For i = 0 To s.Length - 1
Console.Write("{0,-5} {1,-3}", i, s(i))
Console.Write(" {0,-5}", CharUnicodeInfo.GetNumericValue(s, i))
Console.Write(" {0,-5}", CharUnicodeInfo.GetDigitValue(s, i))
Console.Write(" {0,-5}", CharUnicodeInfo.GetDecimalDigitValue(s, i))
Console.WriteLine("{0}", CharUnicodeInfo.GetUnicodeCategory(s, i))
Next i
End Sub
End Class
'This code produces the following output. Some characters might not display at the console.
'
'String: a9Γ²¼௯௰➈
'index c Num Dig Dec UnicodeCategory
'0 a -1 -1 -1 LowercaseLetter
'1 9 9 9 9 DecimalDigitNumber
'2 Γ -1 -1 -1 UppercaseLetter
'3 ² 2 2 -1 OtherNumber
'4 ¼ 0.25 -1 -1 OtherNumber
'5 ௯ 9 9 9 DecimalDigitNumber
'6 ௰ 10 -1 -1 OtherNumber
'7 ➈ 9 9 -1 OtherNumber
備註
數值是 Unicode 字元屬性,僅適用於數值字元,其中包括分數、下標、上標、羅馬數位、貨幣 Numerator、列舉數位和腳本特定的數位。 如需 Unicode 字元的詳細資訊,請參閱 Unicode Standard。
Char如果位於位置index
的物件是有效 Surrogate 字組的第一個字元,GetNumericValue(String, Int32)此方法會判斷 Surrogate 字組是否形成數值,如果是,則會傳回其數值。 例如,「啟用編號」系統是由代碼點 U+10107 到 U+10133 所組成。 下列範例會 ConvertFromUtf32 使用 方法來具現化代表每一個「快取編號」的字串。 如範例的輸出所示, GetNumericValue(String, Int32) 如果方法傳遞了一個素數的高 Surrogate,此方法會傳回正確的數值。 不過,如果傳遞低 Surrogate,它只會考慮隔離中的低 Surrogate,並傳回 -1。
// Define a UTF32 value for each character in the
// Aegean numbering system.
for (int utf32 = 0x10107; utf32 <= 0x10133; utf32++) {
string surrogate = Char.ConvertFromUtf32(utf32);
for (int ctr = 0; ctr < surrogate.Length; ctr++)
Console.Write("U+{0:X4} at position {1}: {2} ",
Convert.ToUInt16(surrogate[ctr]), ctr,
System.Globalization.CharUnicodeInfo.GetNumericValue(surrogate, ctr));
Console.WriteLine();
}
// The example displays the following output:
// U+D800 at position 0: 1 U+DD07 at position 1: -1
// U+D800 at position 0: 2 U+DD08 at position 1: -1
// U+D800 at position 0: 3 U+DD09 at position 1: -1
// U+D800 at position 0: 4 U+DD0A at position 1: -1
// U+D800 at position 0: 5 U+DD0B at position 1: -1
// U+D800 at position 0: 6 U+DD0C at position 1: -1
// U+D800 at position 0: 7 U+DD0D at position 1: -1
// U+D800 at position 0: 8 U+DD0E at position 1: -1
// U+D800 at position 0: 9 U+DD0F at position 1: -1
// U+D800 at position 0: 10 U+DD10 at position 1: -1
// U+D800 at position 0: 20 U+DD11 at position 1: -1
// U+D800 at position 0: 30 U+DD12 at position 1: -1
// U+D800 at position 0: 40 U+DD13 at position 1: -1
// U+D800 at position 0: 50 U+DD14 at position 1: -1
// U+D800 at position 0: 60 U+DD15 at position 1: -1
// U+D800 at position 0: 70 U+DD16 at position 1: -1
// U+D800 at position 0: 80 U+DD17 at position 1: -1
// U+D800 at position 0: 90 U+DD18 at position 1: -1
// U+D800 at position 0: 100 U+DD19 at position 1: -1
// U+D800 at position 0: 200 U+DD1A at position 1: -1
// U+D800 at position 0: 300 U+DD1B at position 1: -1
// U+D800 at position 0: 400 U+DD1C at position 1: -1
// U+D800 at position 0: 500 U+DD1D at position 1: -1
// U+D800 at position 0: 600 U+DD1E at position 1: -1
// U+D800 at position 0: 700 U+DD1F at position 1: -1
// U+D800 at position 0: 800 U+DD20 at position 1: -1
// U+D800 at position 0: 900 U+DD21 at position 1: -1
// U+D800 at position 0: 1000 U+DD22 at position 1: -1
// U+D800 at position 0: 2000 U+DD23 at position 1: -1
// U+D800 at position 0: 3000 U+DD24 at position 1: -1
// U+D800 at position 0: 4000 U+DD25 at position 1: -1
// U+D800 at position 0: 5000 U+DD26 at position 1: -1
// U+D800 at position 0: 6000 U+DD27 at position 1: -1
// U+D800 at position 0: 7000 U+DD28 at position 1: -1
// U+D800 at position 0: 8000 U+DD29 at position 1: -1
// U+D800 at position 0: 9000 U+DD2A at position 1: -1
// U+D800 at position 0: 10000 U+DD2B at position 1: -1
// U+D800 at position 0: 20000 U+DD2C at position 1: -1
// U+D800 at position 0: 30000 U+DD2D at position 1: -1
// U+D800 at position 0: 40000 U+DD2E at position 1: -1
// U+D800 at position 0: 50000 U+DD2F at position 1: -1
// U+D800 at position 0: 60000 U+DD30 at position 1: -1
// U+D800 at position 0: 70000 U+DD31 at position 1: -1
// U+D800 at position 0: 80000 U+DD32 at position 1: -1
// U+D800 at position 0: 90000 U+DD33 at position 1: -1
' Define a UTF32 value for each character in the
' Aegean numbering system.
For utf32 As Integer = &h10107 To &h10133
Dim surrogate As String = Char.ConvertFromUtf32(utf32)
For ctr As Integer = 0 To surrogate.Length - 1
Console.Write("U+{0:X4} at position {1}: {2} ",
Convert.ToUInt16(surrogate(ctr)), ctr,
System.Globalization.CharUnicodeInfo.GetNumericValue(surrogate, ctr))
Next
Console.WriteLine()
Next
' The example displays the following output:
' U+D800 at position 0: 1 U+DD07 at position 1: -1
' U+D800 at position 0: 2 U+DD08 at position 1: -1
' U+D800 at position 0: 3 U+DD09 at position 1: -1
' U+D800 at position 0: 4 U+DD0A at position 1: -1
' U+D800 at position 0: 5 U+DD0B at position 1: -1
' U+D800 at position 0: 6 U+DD0C at position 1: -1
' U+D800 at position 0: 7 U+DD0D at position 1: -1
' U+D800 at position 0: 8 U+DD0E at position 1: -1
' U+D800 at position 0: 9 U+DD0F at position 1: -1
' U+D800 at position 0: 10 U+DD10 at position 1: -1
' U+D800 at position 0: 20 U+DD11 at position 1: -1
' U+D800 at position 0: 30 U+DD12 at position 1: -1
' U+D800 at position 0: 40 U+DD13 at position 1: -1
' U+D800 at position 0: 50 U+DD14 at position 1: -1
' U+D800 at position 0: 60 U+DD15 at position 1: -1
' U+D800 at position 0: 70 U+DD16 at position 1: -1
' U+D800 at position 0: 80 U+DD17 at position 1: -1
' U+D800 at position 0: 90 U+DD18 at position 1: -1
' U+D800 at position 0: 100 U+DD19 at position 1: -1
' U+D800 at position 0: 200 U+DD1A at position 1: -1
' U+D800 at position 0: 300 U+DD1B at position 1: -1
' U+D800 at position 0: 400 U+DD1C at position 1: -1
' U+D800 at position 0: 500 U+DD1D at position 1: -1
' U+D800 at position 0: 600 U+DD1E at position 1: -1
' U+D800 at position 0: 700 U+DD1F at position 1: -1
' U+D800 at position 0: 800 U+DD20 at position 1: -1
' U+D800 at position 0: 900 U+DD21 at position 1: -1
' U+D800 at position 0: 1000 U+DD22 at position 1: -1
' U+D800 at position 0: 2000 U+DD23 at position 1: -1
' U+D800 at position 0: 3000 U+DD24 at position 1: -1
' U+D800 at position 0: 4000 U+DD25 at position 1: -1
' U+D800 at position 0: 5000 U+DD26 at position 1: -1
' U+D800 at position 0: 6000 U+DD27 at position 1: -1
' U+D800 at position 0: 7000 U+DD28 at position 1: -1
' U+D800 at position 0: 8000 U+DD29 at position 1: -1
' U+D800 at position 0: 9000 U+DD2A at position 1: -1
' U+D800 at position 0: 10000 U+DD2B at position 1: -1
' U+D800 at position 0: 20000 U+DD2C at position 1: -1
' U+D800 at position 0: 30000 U+DD2D at position 1: -1
' U+D800 at position 0: 40000 U+DD2E at position 1: -1
' U+D800 at position 0: 50000 U+DD2F at position 1: -1
' U+D800 at position 0: 60000 U+DD30 at position 1: -1
' U+D800 at position 0: 70000 U+DD31 at position 1: -1
' U+D800 at position 0: 80000 U+DD32 at position 1: -1
' U+D800 at position 0: 90000 U+DD33 at position 1: -1