## 3 replies

Last post Feb 04, 2015 01:26 PM by gerrylowry

• neoaguil17

Member

158 Points

517 Posts

### Math.pow with decimals

Hello

I need to a create function:

```public static decimal Pow(
decimal x,
decimal y
)But I don't find how do it...```
• gerrylowry

Star

14297 Points

5797 Posts

### Re: Math.pow with decimals

Here is the definition for System.Math.Pow

```public static double Pow( // https://msdn.microsoft.com/en-us/library/system.math.pow(v=vs.110).aspx
double x,
double y
)```

You could piggy back on the above; keep in mind that exceptions are possible.

```public static decimal DecimalPower(decimal x,	decimal y)
{
Double X = (double)x;
Double Y = (double)y;
return (decimal)System.Math.Pow(X, Y);
}```

edit:      example

```void Main()
{
Decimal p = (Decimal)Math.Sqrt(2.0);
Console.WriteLine(p);
Decimal y = 2.0M;
Console.WriteLine(DecimalPower(p,y));
}```

output:

```1.4142135623731
2.00000000000001```

end edit.

B-)  Gerry Lowry, Chief Training Architect, Paradigm Mentors Learning never ends... +1 705-999-9195 wasaga beach, ontario canada TIMTOWTDI =.there is more than one way to do it
• Paul Linton

Star

9555 Points

2784 Posts

### Re: Math.pow with decimals

Feb 02, 2015 09:57 PM|Paul Linton|LINK

#### neoaguil17

But I don't find how do it...

1) What is it that you do not know how to do?

2) Do you know how to write a method that returns a result?

3) Can you write a method that adds two numbers?

4) Is this an exercise from your text book or an assignment from your tutor?

5) Have you done the previous exercises?

Got a c# problem? Try .NET Book Zero from Charles Petzold, it's a free pdf.
• gerrylowry

Star

14297 Points

5797 Posts