[chronojump] Missed on last commit



commit d301fe299e29e0187025318cb590a00dc0a2ed69
Author: Xavier de Blas <xaviblas gmail com>
Date:   Wed Jan 8 16:18:36 2020 +0100

    Missed on last commit

 src/utilAll.cs | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
---
diff --git a/src/utilAll.cs b/src/utilAll.cs
index 2c628435..0cc3b216 100644
--- a/src/utilAll.cs
+++ b/src/utilAll.cs
@@ -191,5 +191,41 @@ public class UtilAll
                return path;
        }
 
+       //avoids divide by zero
+       //thought for being between 0, 1
+       //ideal for progressBars
+       public static double DivideSafeFraction (double num, double denom)
+       {
+               if(num == 0 || denom == 0)
+                       return 0;
+
+               double result = num / denom;
+
+               if(result > 1)
+                       result = 1;
+               else if(result < 0)
+                       result = 0;
+
+               return result;
+       }
+
+       public static double DivideSafeFraction (int val1, int val2) {
+               return DivideSafeFraction(Convert.ToDouble(val1), Convert.ToDouble(val2));
+       }
+
+       //Not restricted to values 0-1
+       public static double DivideSafe (double val1, double val2)
+       {
+               if(val1 == 0 || val2 == 0)
+                       return 0;
+
+               return val1 / val2;
+       }
+
+       public static int DivideSafeAndGetInt (double val1, double val2)
+       {
+               return Convert.ToInt32(DivideSafe(val1, val2));
+       }
+
 
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]