C# has a built-in Math
class that provides useful mathematical functions and operations. The class has the Clamp()
function, which is used to return a value clamped to an inclusive range of two specified numbers.
This function returns the value
clamped to the inclusive range of min_param
and max_param
.
Clamp (type value, type min_param, type max_param);
where:
value
: is the value to be clampedmin_param
: is the lower bound of resultmax_param
: is the upper bound of resultvalue
: Returns this if min_param ≤ value ≤ max_param
or
min_param
: Returns this if value < min_param
or
max_param
: Returns this if value > max_param
public static float Clamp (float value, float min_param, float max_param);
public static double Clamp (double value, double min_param, double max_param);
public static decimal Clamp (decimal value, decimal min_param, decimal max_param);
public static byte Clamp (byte value, byte min_param, byte max_param);
public static ushort Clamp (ushort value, ushort min_param, ushort max_param);
public static uint Clamp (uint value, uint min_param, uint max_param);
public static ulong Clamp (ulong value, ulong min_param, ulong max_param);
public static sbyte Clamp (sbyte value, sbyte min_param, sbyte max_param);
public static short Clamp (short value, short min_param, short max_param);
public static int Clamp (int value, int min_param, int max_param);
public static long Clamp (long value, long min_param, long max_param);
using System;class HelloWorld{static void Main(){System.Console.WriteLine(Math.Clamp(5,0,4));System.Console.WriteLine(Math.Clamp(1,4,6));System.Console.WriteLine(Math.Clamp(5,0,6));}}