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:
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:
The following function
Returns a
double
. I see that you have tried to define afloat
of named
to the output fromMath.Round(n,2)
wheren
is a double of value1.12345
and2
represents an integer using the following codeYou'll actually get an error because the output from the above function is
double
and not afloat
.You may fix this by changing
float d = Math.Round(n,2);
todouble d = Math.Round(n,2);
Thanks,
I hope you find this helpful :)
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.