Avatar of melegant99
melegant99
 asked on

How to Cast Decimal to Double?

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

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

So, what the smeg gives?
C#.NET Programming

Avatar of undefined
Last Comment
melegant99

8/22/2022 - Mon
abel

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.
abel

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()?
abel

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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
melegant99

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

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

The error is happening at runtime.
ASKER CERTIFIED SOLUTION
abel

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
melegant99

ASKER
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.)

melegant99

ASKER
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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
melegant99

ASKER
what I really don't understand is why a cast to Decimal is no issue, but to Double it is?

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

melegant99

ASKER
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?
abel

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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
melegant99

ASKER
Thanks Abel.