Click here to Skip to main content
14,301,033 members
   

C#

 
GeneralRe: Excel's MOD in C#? Pin
Peter_in_278018-Aug-19 21:58
professionalPeter_in_278018-Aug-19 21:58 
GeneralRe: Excel's MOD in C#? Pin
Jassim Rahma18-Aug-19 22:18
memberJassim Rahma18-Aug-19 22:18 
GeneralRe: Excel's MOD in C#? Pin
Peter_in_278018-Aug-19 22:25
professionalPeter_in_278018-Aug-19 22:25 
AnswerRe: Excel's MOD in C#? Pin
OriginalGriff18-Aug-19 22:08
protectorOriginalGriff18-Aug-19 22:08 
GeneralRe: Excel's MOD in C#? Pin
Jassim Rahma18-Aug-19 22:22
memberJassim Rahma18-Aug-19 22:22 
GeneralRe: Excel's MOD in C#? Pin
OriginalGriff18-Aug-19 22:28
protectorOriginalGriff18-Aug-19 22:28 
GeneralRe: Excel's MOD in C#? Pin
BillWoodruff19-Aug-19 21:48
mveBillWoodruff19-Aug-19 21:48 
AnswerRe: Excel's MOD in C#? Pin
BillWoodruff19-Aug-19 23:43
mveBillWoodruff19-Aug-19 23:43 
what I use:
using System;
using System.Runtime.InteropServices.WindowsRuntime;

namespace Utilities
{
    public static class MathExtensions
    {
        // ideas from: Lippert, Gravell, Skeet
        public static double ModT<T>(this T i1, T i2)
        {
            double v1 = i1.GetDouble();
            double v2 = i2.GetDouble();
            return v1 - v2 * Math.Floor(v1 / v2);
        }

        public static double ModT1T2<T1, T2>(this T1 i1, T2 i2)
        {
            double v1 = i1.GetDouble();
            double v2 = i2.GetDouble();
            return v1 - v2 * Math.Floor(v1 / v2);
        }

        public static double GetDouble<T>(this T i1)
        {
            double v1;

            try
            {
                v1 = Convert.ToDouble(i1);
            }
            catch (InvalidCastException iex)
            {
                throw new InvalidCastException($"type {typeof(T)} cannot be cast to double: {iex.Message}");
            }

            catch (FormatException fex)
            {
                throw new FormatException($"type {typeof(T)} cannot be used: {fex.Message}");
            }

            catch (OverflowException oex)
            {
                throw new OverflowException($"type {typeof(T)} result is an overflow: {oex.Message}");
            }

            return v1;
        }
    }
}
Tests:
double dbl = -1.779406909.ModT(2 * Math.PI);

double int1 = 221.ModT(20);

double sngl = 102.0f.ModT(20.0f);

double deci = 102.0m.ModT(20.0m);

double intdecicombo = 100.0.ModT1T2(34.56m);

double dbl2 = 100.0.ModT(34.56);

«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

SuggestionRe: Excel's MOD in C#? Pin
Richard Deeming20-Aug-19 1:39
mveRichard Deeming20-Aug-19 1:39 
GeneralRe: Excel's MOD in C#? Pin
BillWoodruff20-Aug-19 3:07
mveBillWoodruff20-Aug-19 3:07 
GeneralRe: Excel's MOD in C#? Pin
Richard Deeming20-Aug-19 3:23
mveRichard Deeming20-Aug-19 3:23 
GeneralRe: Excel's MOD in C#? Pin
BillWoodruff20-Aug-19 4:42
mveBillWoodruff20-Aug-19 4:42 
GeneralRe: Excel's MOD in C#? Pin
Richard Deeming20-Aug-19 5:27
mveRichard Deeming20-Aug-19 5:27 
Questionc# Pin
Member 1456180618-Aug-19 5:22
memberMember 1456180618-Aug-19 5:22 
AnswerRe: c# Pin
OriginalGriff18-Aug-19 6:35
protectorOriginalGriff18-Aug-19 6:35 
AnswerRe: c# Pin
#realJSOP21-Aug-19 1:38
mve#realJSOP21-Aug-19 1:38 
QuestionZKTeco merge 3 finger print C# with ZKFingerSDK Pin
Rikus Marais14-Aug-19 2:33
memberRikus Marais14-Aug-19 2:33 
AnswerRe: ZKTeco merge 3 finger print C# with ZKFingerSDK Pin
OriginalGriff14-Aug-19 4:08
protectorOriginalGriff14-Aug-19 4:08 
GeneralRe: ZKTeco merge 3 finger print C# with ZKFingerSDK Pin
Richard Deeming14-Aug-19 7:56
mveRichard Deeming14-Aug-19 7:56 
AnswerRe: ZKTeco merge 3 finger print C# with ZKFingerSDK Pin
Gerry Schmitz15-Aug-19 4:02
mveGerry Schmitz15-Aug-19 4:02 
GeneralRe: ZKTeco merge 3 finger print C# with ZKFingerSDK Pin
OriginalGriff15-Aug-19 4:12
protectorOriginalGriff15-Aug-19 4:12 
GeneralRe: ZKTeco merge 3 finger print C# with ZKFingerSDK Pin
Richard Deeming15-Aug-19 4:33
mveRichard Deeming15-Aug-19 4:33 
GeneralRe: ZKTeco merge 3 finger print C# with ZKFingerSDK Pin
Gerry Schmitz15-Aug-19 9:26
mveGerry Schmitz15-Aug-19 9:26 
QuestionHow i can convert perl scripts to exe file to use in c#? Pin
Member 1455624211-Aug-19 1:25
memberMember 1455624211-Aug-19 1:25 
AnswerRe: How i can convert perl scripts to exe file to use in c#? Pin
OriginalGriff11-Aug-19 1:28
protectorOriginalGriff11-Aug-19 1:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.