Search code examples
c#pownumericupdown

C# Decimal to double?


Why can't I use math.pow with numericUpDown value? When I use this code:

a = (numericUpDown1.Value);
b = (numericUpDown2.Value);
m = Math.Pow(a, b);

I receive the following error:

Severity Code Description Project File Line Suppression State
Error CS0266 Cannot implicitly convert type 'decimal' to 'double'. An explicit conversion exists (are you missing a cast?)  

Solution

  • This is because Math.Pow has only one overload - the one accepting two doubles. Since you want to work with decimals, you need to use explicit casts in both directions:

    decimal m = (decimal)Math.Pow((double)a, (double)b);