## C# Math.Clamp() – Examples

In this tutorial, we will learn about the C# Math.Clamp() method, and learn how to use this method to clamp a value if it is out of the given range [min, max] with the help of examples.

## Clamp(Byte value, Byte min, Byte max)

Math.Clamp(value, min, max) returns `value` clamped to the inclusive range of `min` and `max` where `value`, `min` and `max` are values of type Byte.

If `value` lies in between `min` and `max`, i.e., `min <= value <= max`, then Clamp() method returns `value` as result.

If `value` is less than `min`, i.e., `value <= min <= max`, then Clamp() method returns `min` as result.

If `value` is greater than `max`, i.e., `min <= max <= value`, then Clamp() method returns `max` as result.

### Syntax

The syntax of Clamp() method is

`Math.Clamp(Byte value, Byte min, Byte max)`

where

Return Value

The method returns clamped value of type Byte.

### Example 1 – Clamp(value, min, max)

In this example, we will take specific values for min and max; and find the clamping values for some Byte values, using Math.Clamp() method.

C# Program

```using System;

class Example {
static void Main(string[] args) {
Byte value, min, max, result;

min = 2;
max = 8;
value = 5; // min <= value <= max
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");

min = 2;
max = 8;
value = 1; // value <= min <= max
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");

min = 2;
max = 8;
value = 9; // min <= max <= value
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");
}
}```

Output

```Clamp(5, 2, 8) = 5
Clamp(1, 2, 8) = 2
Clamp(9, 2, 8) = 8```

## Clamp(Decimal, Decimal, Decimal)

Math.Clamp(value, min, max) returns `value` clamped to the inclusive range of `min` and `max` where `value`, `min` and `max` are values of type Decimal.

### Syntax

The syntax of Clamp() method is

`Math.Clamp(Decimal value, Decimal min, Decimal max)`

where

Return Value

The method returns clamped value of type Decimal.

### Example 2 – Clamp(Decimal, Decimal, Decimal)

In this example, we will take specific values for min and max; and find the clamping values for some Decimal values, using Math.Clamp() method.

C# Program

```using System;

class Example {
static void Main(string[] args) {
Decimal value, min, max, result;

min = 2.2M;
max = 8.1M;
value = 5.12M; // min <= value <= max
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");

min = 2.2M;
max = 8.1M;
value = 1.4M; // value <= min <= max
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");

min = 2.2M;
max = 8.4M;
value = 9M; // min <= max <= value
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");
}
}```

Output

```Clamp(5.12, 2.2, 8.1) = 5.12
Clamp(1.4, 2.2, 8.1) = 2.2
Clamp(9, 2.2, 8.4) = 8.4```

## Clamp(Double, Double, Double)

Math.Clamp(value, min, max) returns `value` clamped to the inclusive range of `min` and `max` where `value`, `min` and `max` are values of type Double.

### Syntax

The syntax of Clamp() method is

`Math.Clamp(Double value, Double min, Double max)`

where

Return Value

The method returns clamped value of type Double.

### Example 3 – Clamp(Double, Double, Double)

In this example, we will take some specific values for min and max; and find the clamping values for some double-point precision floating values, using Math.Clamp() method.

C# Program

```using System;

class Example {
static void Main(string[] args) {
Double value, min, max, result;

min = 2.2;
max = 8.1;
value = 5.12; // min <= value <= max
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");

min = 2.2;
max = 8.1;
value = 1.4; // value <= min <= max
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");

min = 2.2;
max = 8.4;
value = 9; // min <= max <= value
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");
}
}```

Output

```Clamp(5.12, 2.2, 8.1) = 5.12
Clamp(1.4, 2.2, 8.1) = 2.2
Clamp(9, 2.2, 8.4) = 8.4```

## Clamp(Int16, Int16, Int16)

Math.Clamp(value, min, max) returns `value` clamped to the inclusive range of `min` and `max` where `value`, `min` and `max` are values of type Int16 (or short).

### Syntax

The syntax of Clamp() method is

`Math.Clamp(Int16 value, Int16 min, Int16 max)`

where

Return Value

The method returns clamped value of type Int16.

### Example 4 – Clamp(Int16, Int16, Int16)

In this example, we will take some specific values for min and max; and find the clamping values for some 16-bit signed integers, using Math.Clamp() method.

C# Program

```using System;

class Example {
static void Main(string[] args) {
Int16 value, min, max, result;

min = 2;
max = 8;
value = 5; // min <= value <= max
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");

min = 2;
max = 8;
value = 1; // value <= min <= max
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");

min = 2;
max = 8;
value = 9; // min <= max <= value
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");
}
}```

Output

```Clamp(5, 2, 8) = 5
Clamp(1, 2, 8) = 2
Clamp(9, 2, 8) = 8```

## Clamp(Int32, Int32, Int32)

Math.Clamp(value, min, max) returns `value` clamped to the inclusive range of `min` and `max` where `value`, `min` and `max` are values of type Int32 (or int).

### Syntax

The syntax of Clamp() method is

`Math.Clamp(Int32 value, Int32 min, Int32 max)`

where

Return Value

The method returns clamped value of type Int32.

### Example 5 – Clamp(Int32, Int32, Int32)

In this example, we will take some specific values for min and max; and find the clamping values for some 32-bit signed integers, using Math.Clamp() method.

C# Program

```using System;

class Example {
static void Main(string[] args) {
Int32 value, min, max, result;

min = 2;
max = 8;
value = 5; // min <= value <= max
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");

min = 2;
max = 8;
value = 1; // value <= min <= max
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");

min = 2;
max = 8;
value = 9; // min <= max <= value
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");
}
}```

Output

```Clamp(5, 2, 8) = 5
Clamp(1, 2, 8) = 2
Clamp(9, 2, 8) = 8```

## Clamp(Int64, Int64, Int64)

Math.Clamp(value, min, max) returns `value` clamped to the inclusive range of `min` and `max` where `value`, `min` and `max` are values of type Int64 (or long).

### Syntax

The syntax of Clamp() method is

`Math.Clamp(Int64 value, Int64 min, Int64 max)`

where

Return Value

The method returns clamped value of type Int64.

### Example 6 – Clamp(Int64, Int64, Int64)

In this example, we will take some specific values for min and max; and find the clamping values for some 64-bit signed integers, using Math.Clamp() method.

C# Program

```using System;

class Example {
static void Main(string[] args) {
Int64 value, min, max, result;

min = 2;
max = 8;
value = 5; // min <= value <= max
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");

min = 2;
max = 8;
value = 1; // value <= min <= max
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");

min = 2;
max = 8;
value = 9; // min <= max <= value
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");
}
}```

Output

```Clamp(5, 2, 8) = 5
Clamp(1, 2, 8) = 2
Clamp(9, 2, 8) = 8```

## Clamp(SByte, SByte, SByte)

Math.Clamp(value, min, max) returns `value` clamped to the inclusive range of `min` and `max` where `value`, `min` and `max` are values of type SByte (8-bit signed integer).

### Syntax

The syntax of Clamp() method is

`Math.Clamp(SByte value, SByte min, SByte max)`

where

Return Value

The method returns clamped value of type SByte.

### Example 7 – Clamp(SByte, SByte, SByte)

In this example, we will take some specific values for min and max; and find the clamping values for some 8-bit signed integers, using Math.Clamp() method.

C# Program

```using System;

class Example {
static void Main(string[] args) {
SByte value, min, max, result;

min = 2;
max = 8;
value = 5; // min <= value <= max
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");

min = 2;
max = 8;
value = 1; // value <= min <= max
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");

min = 2;
max = 8;
value = 9; // min <= max <= value
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");
}
}```

Output

```Clamp(5, 2, 8) = 5
Clamp(1, 2, 8) = 2
Clamp(9, 2, 8) = 8```

## Clamp(Single, Single, Single)

Math.Clamp(value, min, max) returns `value` clamped to the inclusive range of `min` and `max` where `value`, `min` and `max` are values of type Single.

### Syntax

The syntax of Clamp() method is

`Math.Clamp(Single value, Single min,  Single max)`

where

Return Value

The method returns clamped value of type Single.

### Example 8 – Clamp(Single, Single, Single)

In this example, we will take some specific values for min and max; and find the clamping values for some single-precision floating-point numbers, using Math.Clamp() method.

C# Program

```using System;

class Example {
static void Main(string[] args) {
Single value, min, max, result;

min = 2.1F;
max = 8.2F;
value = 5.7F; // min <= value <= max
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");

min = 2.1F;
max = 8.2F;
value = 1.4F; // value <= min <= max
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");

min = 2.1F;
max = 8.2F;
value = 9.3F; // min <= max <= value
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");
}
}```

Output

```Clamp(5.7, 2.1, 8.2) = 5.7
Clamp(1.4, 2.1, 8.2) = 2.1
Clamp(9.3, 2.1, 8.2) = 8.2```

## Clamp(UInt16, UInt16, UInt16)

Math.Clamp(value, min, max) returns `value` clamped to the inclusive range of `min` and `max` where `value`, `min` and `max` are values of type UInt16.

### Syntax

The syntax of Clamp() method is

`Math.Clamp(UInt16 value, UInt16 min, UInt16 max)`

where

Return Value

The method returns clamped value of type UInt16.

### Example 9 – Clamp(UInt16, UInt16, UInt16)

In this example, we will take some specific values for min and max; and find the clamping values for some 16-bit unsigned integers, using Math.Clamp() method.

C# Program

```using System;

class Example {
static void Main(string[] args) {
UInt16 value, min, max, result;

min = 2;
max = 8;
value = 5; // min <= value <= max
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");

min = 2;
max = 8;
value = 1; // value <= min <= max
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");

min = 2;
max = 8;
value = 9; // min <= max <= value
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");
}
}```

Output

```Clamp(5, 2, 8) = 5
Clamp(1, 2, 8) = 2
Clamp(9, 2, 8) = 8```

## Clamp(UInt32, UInt32, UInt32)

Math.Clamp(value, min, max) returns `value` clamped to the inclusive range of `min` and `max` where `value`, `min` and `max` are values of type UInt32.

### Syntax

The syntax of Clamp() method is

`Math.Clamp(UInt32 value, UInt32 min, UInt32 max)`

where

Return Value

The method returns clamped value of type UInt32.

### Example 10 – Clamp(UInt32, UInt32, UInt32)

In this example, we will take some specific values for min and max; and find the clamping values for some 32-bit unsigned integers, using Math.Clamp() method.

C# Program

```using System;

class Example {
static void Main(string[] args) {
UInt32 value, min, max, result;

min = 2;
max = 8;
value = 5; // min <= value <= max
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");

min = 2;
max = 8;
value = 1; // value <= min <= max
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");

min = 2;
max = 8;
value = 9; // min <= max <= value
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");
}
}```

Output

```Clamp(5, 2, 8) = 5
Clamp(1, 2, 8) = 2
Clamp(9, 2, 8) = 8```

## Clamp(UInt64, UInt64, UInt64)

Math.Clamp(value, min, max) returns `value` clamped to the inclusive range of `min` and `max` where `value`, `min` and `max` are values of type UInt64.

### Syntax

The syntax of Clamp() method is

`Math.Clamp(UInt64 value, UInt64 min, UInt64 max)`

where

Return Value

The method returns clamped value of type UInt64.

### Example 11 – Clamp(UInt64, UInt64, UInt64)

In this example, we will take some specific values for min and max; and find the clamping values for some 64-bit unsigned integers, using Math.Clamp() method.

C# Program

```using System;

class Example {
static void Main(string[] args) {
UInt64 value, min, max, result;

min = 2;
max = 8;
value = 5; // min <= value <= max
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");

min = 2;
max = 8;
value = 1; // value <= min <= max
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");

min = 2;
max = 8;
value = 9; // min <= max <= value
result = Math.Clamp(value, min, max);
Console.WriteLine(\$"Clamp({value}, {min}, {max}) = {result}");
}
}```

Output

```Clamp(5, 2, 8) = 5
Clamp(1, 2, 8) = 2
Clamp(9, 2, 8) = 8```

### Conclusion

In this C# Tutorial, we have learnt the syntax of C# Math.Clamp() method, and also learnt how to use this method with the help of C# example programs.