Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Convert.ToDouble(char) not supported?

Tags:

From the msdn page :

public static double ToDouble( char value )

Parameters

value
Type: System.Char The Unicode character to convert.

Return Value
Type: System.Double This conversion is not supported. No value is returned.

If it is not supported, why is it implemented in the first place ?

like image 560
dada686 Avatar asked Apr 25 '12 08:04

dada686


People also ask

How to use Convert ToDouble in c#?

To convert a specified value to a double-precision floating-point number, use Convert. ToDouble() method. long[] val = { 340, -200}; Now convert it to Double.

Can we convert string to double in C#?

String value can be converted to double using Convert. ToDouble() or Double. Parse() method. These methods take string representation of a number as input and return its equivalent double-precision floating-point number.

How do you convert decimal to double?

You can also convert a Decimal to a Double value by using the Explicit assignment operator. Because the conversion can entail a loss of precision, you must use a casting operator in C# or a conversion function in Visual Basic.

How can I convert int to double without overflow?

} // Int to double conversion cannot overflow. doubleVal = System.Convert.ToDouble (intVal); System.Console.WriteLine (" {0} as a double is: {1}", intVal, doubleVal); } Public Sub ConvertDoubleInt (ByVal doubleVal As Double) Dim intVal As Integer = 0 ' Double to Integer conversion can overflow.

Is it possible to convert a char array?

it is also possible, that in some scenarios you are trying to convert a char array eg.: Sign in to answer this question. Unable to complete the action because of changes made to the page.

What is the double value of Int64?

// Converted the Int64 value '9223372036854775807' to the Double value 9.22337203685478E+18.


1 Answers

It is not the only one. Convert.ToBoolean(char), ToDateTime, ToDecimal and ToSingle are also not supported, they all throw InvalidCastException like ToDouble does.

This is just .NET design trying to keep you out of trouble. Converting a char to an integral type is reasonable, you can look at the Unicode mapping tables and count the codepoints. But what would a conversion to Boolean mean? What Unicode code point is True? ToDateTime requires no explanation. How could a character ever be a fractional value? There are no half or quarter codepoints.

You can make it work, convert to Int32 first and then convert to Double. But by all means, check your code and make sure that it is a senseful thing to do. The .NET designers thought it wasn't. They were right.

like image 109
Hans Passant Avatar answered Oct 14 '22 22:10

Hans Passant