andrew4582

Math Utilities

Aug 15th, 2010
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.96 KB | None | 0 0
  1.  
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6.  
  7. namespace System.Utilities
  8. {
  9.   internal class MathUtils
  10.   {
  11.     public static int HexToInt(char h)
  12.     {
  13.       if ((h >= '0') && (h <= '9'))
  14.       {
  15.         return (h - '0');
  16.       }
  17.       if ((h >= 'a') && (h <= 'f'))
  18.       {
  19.         return ((h - 'a') + 10);
  20.       }
  21.       if ((h >= 'A') && (h <= 'F'))
  22.       {
  23.         return ((h - 'A') + 10);
  24.       }
  25.       return -1;
  26.     }
  27.  
  28.     public static char IntToHex(int n)
  29.     {
  30.       if (n <= 9)
  31.       {
  32.         return (char)(n + 48);
  33.       }
  34.       return (char)((n - 10) + 97);
  35.     }
  36.  
  37.     public static int GetDecimalPlaces(double value)
  38.     {
  39.       // increasing max decimal places above 10 produces weirdness
  40.       int maxDecimalPlaces = 10;
  41.       double threshold = Math.Pow(0.1d, maxDecimalPlaces);
  42.  
  43.       if (value == 0.0)
  44.         return 0;
  45.       int decimalPlaces = 0;
  46.       while (value - Math.Floor(value) > threshold && decimalPlaces < maxDecimalPlaces)
  47.       {
  48.         value *= 10.0;
  49.         decimalPlaces++;
  50.       }
  51.       return decimalPlaces;
  52.     }
  53.  
  54.     public static int? Min(int? val1, int? val2)
  55.     {
  56.       if (val1 == null)
  57.         return val2;
  58.       if (val2 == null)
  59.         return val1;
  60.  
  61.       return Math.Min(val1.Value, val2.Value);
  62.     }
  63.  
  64.     public static int? Max(int? val1, int? val2)
  65.     {
  66.       if (val1 == null)
  67.         return val2;
  68.       if (val2 == null)
  69.         return val1;
  70.  
  71.       return Math.Max(val1.Value, val2.Value);
  72.     }
  73.  
  74.     public static double? Min(double? val1, double? val2)
  75.     {
  76.       if (val1 == null)
  77.         return val2;
  78.       if (val2 == null)
  79.         return val1;
  80.  
  81.       return Math.Min(val1.Value, val2.Value);
  82.     }
  83.  
  84.     public static double? Max(double? val1, double? val2)
  85.     {
  86.       if (val1 == null)
  87.         return val2;
  88.       if (val2 == null)
  89.         return val1;
  90.  
  91.       return Math.Max(val1.Value, val2.Value);
  92.     }
  93.   }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment