GetNumericValue Method (Char)
Collapse the table of content
Expand the table of content

CharUnicodeInfo.GetNumericValue Method (Char)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Gets the numeric value associated with the specified character.

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

'Declaration
Public Shared Function GetNumericValue ( _
	ch As Char _
) As Double

Parameters

ch
Type: System.Char
The Unicode character for which to get the numeric value.

Return Value

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

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.

The GetNumericValue method assumes that ch corresponds to a single linguistic character and checks whether that character can be converted to a decimal digit. However, some numbers in the Unicode standard are represented by two Char objects that form a surrogate pair. 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 AEGEAN NUMBER ONE. As the output from the example shows, the GetNumericValue method returns -1 if it is passed either a high surrogate or a low surrogate of this character.


Dim surrogate As String = ChrW(&hD800) + ChrW(&hDD07)     ' AEGEAN NUMBER ONE
For Each ch In surrogate
   outputBlock.Text += String.Format("U+{0:X4}: {1}    ", Convert.ToUInt16(ch), 
                     System.Globalization.CharUnicodeInfo.GetNumericValue(ch)) & vbCrLf
Next
' The example displays the following output:
'       U+D800: -1
'       U+DD07: -1


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)

      outputBlock.Text &= "                                        c  Num   UnicodeCategory" & vbCrLf

      outputBlock.Text &= "U+0061 LATIN SMALL LETTER A            "
      PrintProperties(outputBlock, "a"c)

      outputBlock.Text &= "U+0393 GREEK CAPITAL LETTER GAMMA      "
      PrintProperties(outputBlock, ChrW(&H393))

      outputBlock.Text &= "U+0039 DIGIT NINE                      "
      PrintProperties(outputBlock, "9"c)

      outputBlock.Text &= "U+00B2 SUPERSCRIPT TWO                 "
      PrintProperties(outputBlock, ChrW(&HB2))

      outputBlock.Text &= "U+00BC VULGAR FRACTION ONE QUARTER     "
      PrintProperties(outputBlock, ChrW(&HBC))

      outputBlock.Text &= "U+0BEF TAMIL DIGIT NINE                "
      PrintProperties(outputBlock, ChrW(&HBEF))

      outputBlock.Text &= "U+0BF0 TAMIL NUMBER TEN                "
      PrintProperties(outputBlock, ChrW(&HBF0))

      outputBlock.Text &= "U+0F33 TIBETAN DIGIT HALF ZERO         "
      PrintProperties(outputBlock, ChrW(&HF33))

      outputBlock.Text &= "U+2788 CIRCLED SANS-SERIF DIGIT NINE   "
      PrintProperties(outputBlock, ChrW(&H2788))

   End Sub

   Public Shared Sub PrintProperties(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal c As Char)
      outputBlock.Text += String.Format(" {0,-3}", c)
      outputBlock.Text += String.Format(" {0,-5}", CharUnicodeInfo.GetNumericValue(c))
      outputBlock.Text += String.Format("{0}", CharUnicodeInfo.GetUnicodeCategory(c)) & vbCrLf
   End Sub
End Class 
' This example produces the following output.
'       U+0061 LATIN SMALL LETTER A             a   -1   LowercaseLetter
'       U+0393 GREEK CAPITAL LETTER GAMMA       G   -1   UppercaseLetter
'       U+0039 DIGIT NINE                       9   9    DecimalDigitNumber
'       U+00B2 SUPERSCRIPT TWO                  �   2    OtherNumber
'       U+00BC VULGAR FRACTION ONE QUARTER      �   0.25 OtherNumber
'       U+0BEF TAMIL DIGIT NINE                 ?  9    DecimalDigitNumber
'       U+0BF0 TAMIL NUMBER TEN                 ?   10   OtherNumber
'       U+0F33 TIBETAN DIGIT HALF ZERO          ? -0.5   OtherNumber
'       U+2788 CIRCLED SANS-SERIF DIGIT NINE    ?    9   OtherNumber


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft