Open In App

C# | Char.GetNumericValue() Method

In C#, Char.GetNumericValue() is a System.Char struct method which is used to convert a numeric Unicode character into a double-precision floating-point number. The numeric value must belong to UnicodeCategory, i.e DecimalDigitNumber, LetterNumber, or OtherNumber. This method can be overloaded by passing different type and number of arguments to it.



  1. Char.GetNumericValue(String, Int32) Method
  2. Char.GetNumericValue(Char) Method

Char.GetNumericValue(String, Int32) Method

This method is used to convert the numeric Unicode character at the specified position in a specified string to a double-precision floating point number. Character position in a string always starts from zero.

Syntax:



public static double GetNumericValue(String str, int index);

Parameters:

str: It is the specified string of type System.String.

index: It represents the character position in the string str and type of this parameter is System.Int32.

Return Type: If the character is represented by the number, then this method returns a numeric value at position index in str otherwise it returns -1. The return type of this method is System.Double.

Exceptions:

Example:




// C# program to illustrate the
// Char.GetNumericValue(String, Int32) Method
using System;
  
class GeeksforGeeks {
  
    // Main method
    public static void Main()
    {
  
        // declaration of datatype
        double output;
        string str = "geeks213";
  
        // provide numeric value of position 4
        output = Char.GetNumericValue(str, 4);
        Console.WriteLine(output);
  
        // provide numeric value of position 7
        output = Char.GetNumericValue(str, 7);
        Console.WriteLine(output);
    }
}

Output:
-1
3

Char.GetNumericValue(Char) Method

This method is used to convert the specified Unicode character into a double-precision floating-point number.

Syntax:

public static double GetNumericValue(Char ch);

Parameter:

ch: It is the Unicode character which is to be converted into double-precision floating-point number.

Return Type: If a character represents a number, then this method returns the numeric value of ch, otherwise it returns -1.0. The return type of this method is System.Double.

Example:




// C# program to illustrate the
// Char.GetNumericValue(Char) Method
using System;
  
class GeeksforGeeks {
      
    // Main method
    public static void Main()
    {
          
        // using Char.GetNumericValue(char method)
        Console.WriteLine(Char.GetNumericValue('9'));
        Console.WriteLine(Char.GetNumericValue('z'));
    }
}

Output:
9
-1

Reference: https://docs.microsoft.com/en-us/dotnet/api/system.char.getnumericvalue?view=netframework-4.7.2


Article Tags :
C#