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:

  • If the value of the given string str is null then this method will give ArgumentNullException.
  • If the index is less than zero or greater than the last position in str, then this method will give ArgumentOutOfRangeException.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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'));
    }
}

chevron_right


Output:

9
-1

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.