Casting from a double to float -- please help!

A

almurph

Hi,


I'm casting from double to float in C# (yes, I know I will loose
precision but that is okay) but I am having a problem as follows:


double d = 8.5545888281710365;
float f = (float)d;


problem is though - I'm getting 0.0 for the value of the float!
Anyone with any ideas/suggestions as to why this is occuring? Would
appreciate any comments that you are able to offer in this regard.

Thank you,
Al.
 
A

Alberto Poblacion

I'm casting from double to float in C# (yes, I know I will loose
precision but that is okay) but I am having a problem as follows:

double d = 8.5545888281710365;
float f = (float)d;

problem is though - I'm getting 0.0 for the value of the float!

I wrote, compiled and ran the following code:

using System;
class Test
{
public static void Main()
{
double d = 8.5545888281710365;
float f = (float)d;
Console.WriteLine(f);
}
}

It produces the following output:
8.554589

So it is working as expected. Whichever problem is happening in your code is
occurring somewhere else, and not in the two lines that you posted.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top