0

In .NET Library there is Function like
System.Math.Round(double, int)

But why I need to cast double value to float to make it work..??
Look on the following screenshot:

enter image description here

shashwat
  • 7,851
  • 9
  • 57
  • 90
  • http://stackoverflow.com/questions/618535/what-is-the-difference-between-decimal-float-and-double-in-c. Is this what u ask for? – Mullaly Nov 01 '12 at 06:27
  • I had already looked on this. This is not 4 wht i m lukin 4..? @Mullaly – shashwat Nov 01 '12 at 06:29
  • Is your question why there is no implicit cast from double to float? – Mullaly Nov 01 '12 at 06:34
  • Actually my mistake. I assumed that the error is in function calling. But that is not. It is because I m storing its returned value in float @Mullaly – shashwat Nov 01 '12 at 06:38

2 Answers2

6

The following function

Math.Round(double value, int digits)

Math.Round(double value, int digits) returns a double

Returns a double. I see that you have tried to define a float of name d to the output from Math.Round(n,2) where n is a double of value 1.12345 and 2 represents an integer using the following code

double n = 1.12345;
float d = Math.Round(n,2);

You'll actually get an error because the output from the above function is double and not a float.

Cannot implictly convert type 'double' to 'float'. An explicit conversion exists (are you missing a cast?)

You may fix this by changing float d = Math.Round(n,2); to double d = Math.Round(n,2);

Thanks,
I hope you find this helpful :)

Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
1

Converting from double to float, you will lose precision and it cannot be done implicitly. If you assign a float value to a double variable which is more accurate, the compiler will not complain.

lawrencexu
  • 13
  • 4