I have a problem rounding a float value. I have a float value that is set 0.8 but in the c# code it is somehow 0.80000000000000004.
So i want to round it so it becomes 0.8.
i have tried:
float roundedFloatvalue = (float)Math.Round((Decimal)floatvalue, 2, MidpointRounding.AwayFromZero);
and:
float roundedFloatvalue = Truncate(floatvalue, 2);
public static float Truncate(float value, int digits)
{
double mult = Math.Pow(10.0, digits);
double result = Math.Truncate(mult * value) / mult;
return (float)result;
}
It just seems that i cant get 0.80000000000000004 to be 0.8 and i dont know why any of the above doesnt work.