I have a function as under
private double RoundOff(object value)
{
return Math.Round((double)value, 2);
}
And I am invoking it as under
decimal dec = 32.464762931906M;
var res = RoundOff(dec);
I am gettingthe below error
Specified cast is not valid
What is the mistake?
Thanks
Casting the
objecttodoublewill attempt to unbox the object as a double, but the boxed object is adecimal. You need to convert it to a double after first unboxing it. Then you perform the rounding:Math.Round((double)(decimal)value, 2);