Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Char.GetNumericValue() Method in C#
The Char.GetNumericValue() method in C# converts a specified Unicode character to its corresponding numeric value as a double. This method is particularly useful when working with Unicode digits from various scripts and numeral systems.
Syntax
Following is the syntax for the Char.GetNumericValue() method −
public static double GetNumericValue(char ch);
Parameters
-
ch − A Unicode character to convert to its numeric value.
Return Value
The method returns a double representing the numeric value of the character. If the character does not represent a numeric digit, it returns -1.
Using GetNumericValue() with Digit Characters
Example
using System;
public class Demo {
public static void Main() {
char val = '5';
Console.WriteLine("Character: " + val);
Console.WriteLine("Numeric Value: " + Char.GetNumericValue(val));
// Testing with different numeric characters
char[] digits = {'0', '3', '7', '9'};
foreach (char digit in digits) {
Console.WriteLine("'" + digit + "' ? " + Char.GetNumericValue(digit));
}
}
}
The output of the above code is −
Character: 5 Numeric Value: 5 '0' ? 0 '3' ? 3 '7' ? 7 '9' ? 9
Using GetNumericValue() with Non-Numeric Characters
Example
using System;
public class Demo {
public static void Main() {
char val = 'm';
Console.WriteLine("Character: " + val);
Console.WriteLine("Numeric Value: " + Char.GetNumericValue(val));
// Testing with various non-numeric characters
char[] nonDigits = {'a', 'Z', '@', '#'};
foreach (char ch in nonDigits) {
Console.WriteLine("'" + ch + "' ? " + Char.GetNumericValue(ch));
}
}
}
The output of the above code is −
Character: m Numeric Value: -1 'a' ? -1 'Z' ? -1 '@' ? -1 '#' ? -1
Using GetNumericValue() with Unicode Digits
Example
using System;
public class Demo {
public static void Main() {
// Arabic-Indic digits
char arabicFive = '\u06F5'; // ?
char arabicTwo = '\u06F2'; // ?
Console.WriteLine("Arabic digit ?: " + Char.GetNumericValue(arabicFive));
Console.WriteLine("Arabic digit ?: " + Char.GetNumericValue(arabicTwo));
// Roman numerals
char romanV = '\u2164'; // ? (Roman numeral five)
Console.WriteLine("Roman numeral ?: " + Char.GetNumericValue(romanV));
// Regular ASCII digits for comparison
Console.WriteLine("ASCII digit '8': " + Char.GetNumericValue('8'));
}
}
The output of the above code is −
Arabic digit ?: 5 Arabic digit ?: 2 Roman numeral ?: 5 ASCII digit '8': 8
Common Use Cases
-
Input validation − Check if user input contains valid numeric characters
-
String parsing − Extract numeric values from mixed character strings
-
Unicode processing − Handle numeric characters from different languages and scripts
Conclusion
The Char.GetNumericValue() method provides a reliable way to convert Unicode characters to their numeric values, returning the corresponding double value for digits or -1 for non-numeric characters. It supports various Unicode numeral systems, making it useful for international applications.
