Export (0) Print
Expand All

Char.GetNumericValue Method (String, Int32)

.NET Framework 1.1

Converts the numeric Unicode character at the specified position in a specified string to a double-precision floating point number.

[Visual Basic]
Overloads Public Shared Function GetNumericValue( _
   ByVal s As String, _
   ByVal index As Integer _
) As Double
[C#]
public static double GetNumericValue(
 string s,
 int index
);
[C++]
public: static double GetNumericValue(
 String* s,
 int index
);
[JScript]
public static function GetNumericValue(
   s : String,
 index : int
) : double;

Parameters

s
A String.
index
The character position in s.

Return Value

The numeric value of the character at position index in s if that character represents a number; otherwise, -1.

Exceptions

Exception Type Condition
ArgumentNullException s is a null reference (Nothing in Visual Basic).
ArgumentOutOfRangeException index is less than zero or greater than the last position in s.

Remarks

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.

Example

[Visual Basic, C#, C++] The following sample demonstrates GetNumericValue.

[Visual Basic] 
Imports System

Module GetNumericValueSample

    Sub Main()

        Dim str As String
        str = "input: 1"

        Console.WriteLine(Char.GetNumericValue("8"c))       ' Output: "8"
        Console.WriteLine(Char.GetNumericValue(str, 7))     ' Output: "1"

    End Sub

End Module

[C#] 
using System;

public class GetNumericValueSample {
    public static void Main() {
        string str = "input: 1";

        Console.WriteLine(Char.GetNumericValue('8'));        // Output: "8"
        Console.WriteLine(Char.GetNumericValue(str, 8));    // Output: "1"
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;

int main() {
   String* str = "input: 1";

   Console::WriteLine(Char::GetNumericValue('8'));    // Output: "8"
   Console::WriteLine(Char::GetNumericValue(str, 7)); // Output: "1"
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, Common Language Infrastructure (CLI) Standard

See Also

Char Structure | Char Members | System Namespace | Char.GetNumericValue Overload List

Show:
© 2015 Microsoft