Converting Chars to Doubles in C#

If we’ve got chars with numeric digits in them, we can convert them to numeric (double) values using the char.GetNumericValue method.

The following code:

double d = char.GetNumericValue('5');


Outputs the value: 5

So why does GetNumericValue return a double when a char can only be a single character and hold a single “number” (‘0’ to ‘9’)? Because char holds Unicode characters.

So the following code:

double d = char.GetNumericValue('⅔');


Outputs the value: 0.666666666666667

Hence the need for GetNumericValue to return a double.

 For more C# tips check out my Pluralsight courses: C# Tips and Traps and C# Tips and Traps 2.

You can start watching with a Pluralsight free trial.


Pingbacks and trackbacks (1)+

Add comment