SHARE
TWEET

Clamp Values

ForeverZer0 Feb 16th, 2012 165 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.                 }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top