 We are excited to announce that the ASP.NET Forums are moving to the new Microsoft Q&A experience. Learn more >

# Excel's MOD Formula in ASP.NET RSS

## 10 replies

Last post Aug 20, 2019 11:24 AM by samwu

• ### Excel's MOD Formula in ASP.NET

Aug 18, 2019 06:52 PM|jrahma|LINK

How can I do this Excel formula in C#:

```=MOD(-1.779406909, 2 * PI())
```

Technology News @ www.JassimRahma.com
• ### Re: Excel's MOD Formula in ASP.NET

Aug 18, 2019 11:50 PM|PaulTheSmith|LINK

You should do some verification/testing but I think

-1.7794 - 2*Math.PI  Math.Floor(-1.7794/(2*Math.PI))

should do the trick

• ### Re: Excel's MOD Formula in ASP.NET

Aug 19, 2019 03:42 AM|oned_gk|LINK

`results = -1.779406909 % (2 * Math.PI);`

Programming to simplify, don't look for difficult way
Suwandi - Non Graduate Programmer
• ### Re: Excel's MOD Formula in ASP.NET

Aug 19, 2019 04:52 AM|PaulTheSmith|LINK

#### oned_gk

`results = -1.779406909 % (2 * Math.PI);`

That returns a negative number.  Excel returns a positive number.

• ### Re: Excel's MOD Formula in ASP.NET

Aug 19, 2019 07:29 AM|oned_gk|LINK

to get absolute values (positif), use Math.Abs()

`results = Math.Abs(-1.779406909 % (2 * Math.PI));`

Programming to simplify, don't look for difficult way
Suwandi - Non Graduate Programmer
• ### Re: Excel's MOD Formula in ASP.NET

Aug 19, 2019 07:30 AM|jrahma|LINK

Excel returns

4.503778398

This is the figure I am trying to achieve

But...

`double bearing4 = Math.Floor(-1.7794 / (2 * Math.PI));`

returns -1

and this:

`double bearing4 = -1.779406909 % (2 * Math.PI);`

returns: -1.779406909

Technology News @ www.JassimRahma.com
• ### Re: Excel's MOD Formula in ASP.NET

Aug 19, 2019 07:48 AM|oned_gk|LINK

Seem the result like this

```            Double doublePI = 2 * Math.PI;
Double value = -1.7794;
value = Math.Abs(value);
Double result = doublePI - ( value % doublePI);```

Programming to simplify, don't look for difficult way
Suwandi - Non Graduate Programmer
• ### Re: Excel's MOD Formula in ASP.NET

Aug 19, 2019 07:58 AM|jrahma|LINK

Returns 1.7794

Technology News @ www.JassimRahma.com
• ### Re: Excel's MOD Formula in ASP.NET

Aug 19, 2019 09:00 AM|oned_gk|LINK

```            Double doublePI = 2 * Math.PI;
Double value = -1.7794;
Double result;
if (value<0)
{
result = doublePI + (value % doublePI);
}
else
{
result = value % doublePI;
} ```

Make some test

Programming to simplify, don't look for difficult way
Suwandi - Non Graduate Programmer
• ### Re: Excel's MOD Formula in ASP.NET

Aug 19, 2019 10:44 PM|PaulTheSmith|LINK

My formula gives the correct results for every combination that I checked.  You should do your own testing, of course.

• ### Re: Excel's MOD Formula in ASP.NET

Aug 20, 2019 11:24 AM|samwu|LINK

Hi jrahma,

MOD() function return the remainder after number is divided by divisor in excel. the result has the same sign as divisor.

But there are some differences in C#. For specific practices, you can refer to this formula.

```quotient = floor(dividend / divisor)
mod      = dividend - (divisor * quotient)```

The code:

```            double a = -1.779406909;
double b = 2 * Math.PI;
double c=Math.Floor(a/b);
double d = a - (b*c);
Response.Write(d);```

The result: More information you can refer this link:https://stackoverflow.com/a/35394017

Best regards,

Sam

IIS.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today. Learn more >