Advertisement
ForeverZer0

Clamp Values

Feb 16th, 2012
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.67 KB | None | 0 0
  1.         /// <summary>
  2.         /// Clamps a value between a pair of minimum and maximum values
  3.         /// </summary>
  4.         /// <typeparam name="T">The value type of the object to clamp. Type must use IComparable interface</typeparam>
  5.         /// <param name="val">The value to clamp</param>
  6.         /// <param name="min">The minimum value returned</param>
  7.         /// <param name="max">The maximum value returned</param>
  8.         /// <returns>The clamped value</returns>
  9.             public static T Clamp<T>(T val, T min, T max) where T : IComparable<T>
  10.             {
  11.                     if (val.CompareTo(min) < 0) return min;
  12.                     else if (val.CompareTo(max) > 0) return max;
  13.                     else return val;
  14.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement