Char.GetNumericValue Method (String, Int32)
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Converts the numeric Unicode character at the specified position in a specified string to a double-precision floating point number.
Assembly: mscorlib (in mscorlib.dll)
'Declaration Public Shared Function GetNumericValue ( _ s As String, _ index As Integer _ ) As Double
Return Value
Type: System.DoubleThe numeric value of the character at position index in s if that character represents a number; otherwise, -1.
| Exception | Condition |
|---|---|
| ArgumentNullException | s is Nothing. |
| ArgumentOutOfRangeException | index is less than zero or greater than the last position in s. |
The character in s at position index must be the string representation of a numeric value. For example, if the character at position index in s is '5', the return value is 5. However, if the character at position index in s is 'z', the return value is -1.
Character positions in a string are indexed starting from zero.
A character has an associated numeric value if and only if it is a member of one of the following categories in UnicodeCategory: DecimalDigitNumber, LetterNumber, or OtherNumber.
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. 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, Char.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
The following example demonstrates GetNumericValue.
Module Example Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock) Dim str As String str = "input: 1" outputBlock.Text &= Char.GetNumericValue("8"c) & vbCrLf ' Output: "8" outputBlock.Text += String.Format(Char.GetNumericValue(str, 7)) & vbCrLf ' Output: "1" End Sub End Module