Link to home
Start Free TrialLog in
Avatar of melegant99

asked on

How to Cast Decimal to Double?

I am using a Infragistics Ultragrid.
In a cell update event, I have this:

This throws an exception that specific cast is not valid. If I try
Type t = e.Cell.GetType()
it returns

So, what the smeg gives?
Avatar of abel
Flag of Netherlands image

I'm a bit surprised that the cast fails. Do you get the exception at compile time or at runtime? Normally, the exception shows a text like "cannot convert type 'xxx' to 'double'". What does it say exactly?

My guess is that the runtime type is different.
Are you sure that the code above worked? You tried e.Cell.GetType(), which gave you Decimal, was that System.Decimal or a type from, say, Infragistics (note: a Decimal does not have a .Value property)? Shouldn't it have been e.Cell.Value.GetType()?
e.Cell.Value is of type Object. That means that any cast is valid at compile time. That it doesn't work at runtime is possibly due to the runtime type. From Infragistics documentation I deduct that .Value contains the value from the datasource, the same type. What is the type in the datasource?

When the exception is thrown, the code breaks. At that moment, you can hover over the .Value in the code and Visual Studio will show you the information on that type in the tooltip. Likewise, you can open the Auto-window, which will show you additional information on that object.
Avatar of melegant99


This is what is returned with e.Cell.Value.GetType()....

t = {Name = "Decimal" FullName = "System.Decimal"}

The error is happening at runtime.
Avatar of abel
Flag of Netherlands image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
another thing to add is that there is no info to .Value other than the value itself when I hover..(4M if I click in the box vs 4 when just hovering.)

yes, casting it to just (decimal) does work. and further to that effect, (double)(decimal) works as well.
The exact error is : Specified cast is not valid

I suppose I could work on changing all my values in the app over to decimal, but isn't there better performance with double vs decimal?

Also I used to have the code like this:

if(Double.Parse(e.Cell.Value.ToString()) > 0)
but I feel that a cast has better peformance than a Parse + ToString(), especially when there are HUNDREDS of these operations happening.
what I really don't understand is why a cast to Decimal is no issue, but to Double it is?

double? value = (double?) e.Cell.Value;
gave me the same specific cast is not valid error.

crrrrrrrrrrrrrrrap. I figured it out with your help.
In the database, the fields are set to decimal. If i convert one to float, then the cast works no problem.

Still though, I am not clear on why .NET will not allow a cast since it thinks the value type is a System.Decimal?
It surprised me that a type decimal cannot be converted. If I do:

decimal d = 1;
double dbl = (double) d;

it will compile and run. In your case, the value is an object, but holds (you showed it with GetType) a decimal. The only reason I can think of that the value is not castable is that the type is proxied. But this should become apparent with either GetType or you hovering over the value.

You asked some more questions this weekend. Short answers: performance of decimal vs. double is not an issue, you are using ASP.NET, merely creating the grid is costing so much more time that saving one or two micro-ops on a 100ms operation is not gonna help you much. If you need performance, consider caching the generated HTML using some third party ASP.NET caching mechanism.

The hundredths ToString operations? What do you think happens to get the data to display in the grid in the first place? Yes, it is a costly operation, but again, see it in the light of the bigger picture. Generating HTML, communicating with the database and sending the HTML to the client: those are the parts where you can win something with optimalisation.

Glad you got a stable workaround. Note that the database decimal type is more exact most of the time than the database float type.

-- Abel
Thanks Abel.