Partager via


CharUnicodeInfo.GetNumericValue Method (String, Int32)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Updated: December 2010

Gets the numeric value associated with the character at the specified index of the specified string.

Namespace:  System.Globalization
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Shared Function GetNumericValue ( _
    s As String, _
    index As Integer _
) As Double
public static double GetNumericValue(
    string s,
    int index
)

Parameters

  • s
    Type: System.String
    The String containing the Unicode character for which to get the numeric value.
  • index
    Type: System.Int32
    The index of the Unicode character for which to get the numeric value.

Return Value

Type: System.Double
The numeric value associated with the character at the specified index of the specified string.
-or-
-1, if the character at the specified index of the specified string is not a numeric character.

Exceptions

Exception Condition
ArgumentNullException

s is nulla null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

index is outside the range of valid indexes in s.

Remarks

This property applies only to numeric characters, which include fractions, subscripts, superscripts, Roman numerals, currency numerators, encircled numbers, and script-specific digits.

For more information on Unicode characters, see the Unicode Standard.

If the Char object at position index is the first character of a valid surrogate pair, the GetNumericValue method determines whether the surrogate pair forms a numeric digit, and, if it does, returns its numeric value. For example, the Aegean numbering system consists of code points U+10107 through U+10133. The following example uses the ConvertFromUtf32 method to instantiate a string that represents each Aegean number. As the output from the example shows, the GetNumericValue method returns the correct numeric value if it is passed the high surrogate of an Aegean number. However, if it is passed the low surrogate, it considers only the low surrogate in isolation and returns -1.

' Define each character in the Aegean numbering system.
For code As Integer = &HDD07 To &HDD33
   Dim surrogate As String = ChrW(&HD800) + ChrW(code)
   For ctr As Integer = 0 To surrogate.Length - 1
      outputBlock.Text += String.Format("U+{0:X4} at position {1}: {2}     ", 
                    Convert.ToUInt16(surrogate(ctr)), ctr,  
                    System.Globalization.CharUnicodeInfo.GetNumericValue(surrogate, ctr))
   Next
   outputBlock.Text &= vbCrLf
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
// Define a UTF32 value for each character in the 
// Aegean numbering system.
// Define each character in the // Aegean numbering system.
char high = '\uD800';
for (ushort code = 0xDD07; code <= 0xDD33; code++)
{
   char[] chars = { high, Convert.ToChar(code) };
   string surrogate = new string(chars);
   for (int ctr = 0; ctr < surrogate.Length; ctr++)
      outputBlock.Text += String.Format("U+{0:X4} at position {1}: {2}     ",
                    Convert.ToUInt16(surrogate[ctr]), ctr,
                    System.Globalization.CharUnicodeInfo.GetNumericValue(surrogate, ctr));

   outputBlock.Text += "\n";
}
// 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

Examples

The following code example shows the values returned by each method for different types of characters.

Imports System.Globalization

Public Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      ' The string to get information for.
      Dim s As String = "a9" & ChrW(&h0393) & ChrW(&h00B2) & Chrw(&h00BC) & _
                        ChrW(&h0BEF) & ChrW(&h0BF0) & ChrW(&h2788)
      outputBlock.Text += String.Format("String: {0}", s) & vbCrLf

      ' Print the values for each of the characters in the string.
      outputBlock.Text &= "index c  Num   UnicodeCategory" & vbCrLf
      For i As Integer = 0 To s.Length - 1
         outputBlock.Text += String.Format("{0,-5} {1,-3}", i, s(i))
         outputBlock.Text += String.Format(" {0,-5}", CharUnicodeInfo.GetNumericValue(s, i))
         outputBlock.Text += String.Format("{0}", CharUnicodeInfo.GetUnicodeCategory(s, i)) & vbCrLf
      Next 
   End Sub  
End Class 
' This example produces the following output. 
'       String: a9\u0393\u00B2\u00BC\u0BEF\u0BF0\u2788
'       index c  Num   UnicodeCategory
'       0     a   -1   LowercaseLetter
'       1     9   9    DecimalDigitNumber
'       2     \u0393   -1    UppercaseLetter
'       3     \u00B2   2     OtherNumber
'       4     \u00BC   0.25  OtherNumber
'       5     \u0BEF   9     DecimalDigitNumber
'       6     \u0BF0   10    OtherNumber
'       7     \u2788   9     OtherNumber
using System;
using System.Globalization;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // The string to get information for.
      String s = "a9\u0393\u00B2\u00BC\u0BEF\u0BF0\u2788";
      outputBlock.Text += String.Format("String: {0}", s) + "\n";

      // Print the values for each of the characters in the string.
      outputBlock.Text += "index c  Num   Dig   Dec   UnicodeCategory" + "\n";
      for (int i = 0; i < s.Length; i++)
      {
         outputBlock.Text += String.Format("{0,-5} {1,-3}", i, s[i]);
         outputBlock.Text += String.Format(" {0,-5}", CharUnicodeInfo.GetNumericValue(s, i));
         outputBlock.Text += String.Format("{0}", CharUnicodeInfo.GetUnicodeCategory(s, i)) + "\n";
      }
   }
}
/*
This code produces the following output.  
   String: a9\u0393\u00B2\u00BC\u0BEF\u0BF0\u2788
   index c  Num   UnicodeCategory
   0     a   -1   LowercaseLetter
   1     9   9    DecimalDigitNumber
   2     \u0393   -1    UppercaseLetter
   3     \u00B2   2     OtherNumber
   4     \u00BC   0.25  OtherNumber
   5     \u0BEF   9     DecimalDigitNumber
   6     \u0BF0   10    OtherNumber
   7     \u2788   9     OtherNumber
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Change History

Date

History

Reason

December 2010

Added information about how the method handles surrogate pairs.

Information enhancement.