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

• jrahma

Member

23 Points

683 Posts

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

How can I do this Excel formula in C#:

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

Technology News @ www.JassimRahma.com
• PaulTheSmith

Participant

1660 Points

952 Posts

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

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

• oned_gk

All-Star

52813 Points

15768 Posts

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

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

Programming to simplify, don't look for difficult way
• PaulTheSmith

Participant

1660 Points

952 Posts

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

#### oned_gk

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

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

• oned_gk

All-Star

52813 Points

15768 Posts

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

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
• jrahma

Member

23 Points

683 Posts

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

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
• oned_gk

All-Star

52813 Points

15768 Posts

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

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
• jrahma

Member

23 Points

683 Posts

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

Returns 1.7794

Technology News @ www.JassimRahma.com
• oned_gk

All-Star

52813 Points

15768 Posts

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

```            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
• PaulTheSmith

Participant

1660 Points

952 Posts

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

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

• samwu

Contributor

3370 Points

1409 Posts

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

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: