Can not convert double to float

Posted on 2012-08-20
Medium Priority
Last Modified: 2012-08-26
I am tring to get a double into a the setter of a property which accepts a double.  But I get the compiler error: Can not convert double to float.

  myObject.hours = Convert.ToSingle(_view.NumUpDownMinutes.Value) / 60.0;

How can I get past this problem??
Question by:newbieweb
LVL 16

Accepted Solution

Rose Babu earned 1600 total points
ID: 38311488

can you try like this?

myObject.hours = (float)Convert.ToDouble(Convert.ToSingle(_view.NumUpDownMinutes.Value) / 60.0);

Open in new window

LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 400 total points
ID: 38314596
First of all, why do you Convert.ToSingle before doing the division?

If your property accepts a Double as you state, you should not get the error. The result of a division in C# is a Double. I thus suspect that the property has been defined as a float, not as a Double.

If you have access to the source code for the hours property, then simply redefine it as a Double and you won't have any problem.

Author Closing Comment

ID: 38333937
I had actually been successful using this conversion with multiple nested conversions.  I am not able to change the type in the database, which is what Entity Framework uses to create the objects.

I am up and running...

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question