---
tags:
    - "Max"
    - "Within"
    - "Between"
    - "Min"
    - "Limit"
---

# Clamp

[Math](../index.md) / [Arithmetic](index.md) / **Clamp** Number, Min, Max

<div class="grid cards" markdown>

* :green_circle: Can Read

* :red_circle: Cannot Write

</div>

## Description

Clamps the specified value between Min and Max, both included

## Code

```cs
using System;
using UnityEngine;
using VisualScript.Icons;

namespace VisualScript.Runtime
{
    [Get]
    
    [Title("Clamp")]
    [Category("Math/Arithmetic/Clamp", "Number, Min, Max")]
    
    [Description("Clamps the specified value between Min and Max, both included")]
    [Image(typeof(IconContrast), ColorTheme.Type.Yellow)]
    
    [Keywords("Max", "Within", "Between", "Min", "Limit")]
    
    [Serializable]
    public class TypeNumberMathClamp : TypeNumber
    {
        [SerializeField] private GetNumber m_Number = new GetNumber();
        [SerializeField] private GetNumber m_Min = new GetNumber();
        [SerializeField] private GetNumber m_Max = new GetNumber();

        public override double GetPreview(GameObject source) =>
            Math.Clamp(
                this.m_Number.GetPreview(source),
                this.m_Min.GetPreview(source),
                this.m_Max.GetPreview(source)
            );

        public override double Get(Args args)
        {
            double number = this.m_Number.Get(args);
            double min = this.m_Min.Get(args);
            double max = this.m_Max.Get(args);
            
            return Math.Clamp(number, min, max);
        }

        public override string ToString()
        {
            return $"Clamp({this.m_Number}, {this.m_Min}, {this.m_Max})";
        }
        
        protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
    }
}
```

