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

Member

23 Points

683 Posts

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

How can I do this Excel formula in C#:

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

Participant

1010 Points

536 Posts

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

All-Star

52302 Points

15570 Posts

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

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

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.

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));

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

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);

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

Returns 1.7794

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

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.

1860 Points

707 Posts

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

Member

23 Points

683 Posts

## Excel's MOD Formula in ASP.NET

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

How can I do this Excel formula in C#:

Technology News @ www.JassimRahma.com

Participant

1010 Points

536 Posts

## 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

All-Star

52302 Points

15570 Posts

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

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

Suwandi - Non Graduate Programmer

Participant

1010 Points

536 Posts

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

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

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

All-Star

52302 Points

15570 Posts

## 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()

Suwandi - Non Graduate Programmer

Member

23 Points

683 Posts

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

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

Excel returns

4.503778398This is the figure I am trying to achieve

But...

returns

-1and this:

returns:

-1.779406909Technology News @ www.JassimRahma.com

All-Star

52302 Points

15570 Posts

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

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

Seem the result like this

Suwandi - Non Graduate Programmer

Member

23 Points

683 Posts

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

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

Returns 1.7794

Technology News @ www.JassimRahma.com

All-Star

52302 Points

15570 Posts

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

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

Make some test

Suwandi - Non Graduate Programmer

Participant

1010 Points

536 Posts

## 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.

Participant

1860 Points

707 Posts

## 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.

The code:

The result:

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

Best regards,

Sam

Please remember to click "Mark as Answer" the responses that resolved your issue.

If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.