How to clamp floating numbers in Python Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Clamping is a method in which we limit a number in a range or in between two given numbers. When we clamped a number then it holds the value between the given range. If our clamped number is lower than the minimum value then it holds the lower value and if our number is higher than the maximum value then it holds the higher value. Python doesn't have any function to do the clamping of a number. We need to create our own user-built function to do so. Example 1: Input: 5 Range: [1, 10] Output: 5 Example 2: Input: 5 Range: [10, 20] Output: 10 Example 3: Input: 10 Range: [1, 5] Output: 5Creating user-defined function Now let's implement a clamp(n, min, max) method which takes three parameters where n is the number we would like to clip and the range to be used for clipping the number. Python3 def clamp(n, min, max): if n < min: return min elif n > max: return max else: return n print(clamp(5, 1, 20)) print(clamp(1, 15, 20)) print(clamp(15, 1, 12)) Output: 5 15 12Using the Numpy Inbuilt Method In Numpy as we have a clip method to do clamping of a number. Python3 import numpy as np print(np.clip(10, 6, 20)) Output: 10Using the PyTorch Inbuilt Method In PyTorch as we have a clamp method to do clamping of a number. Python3 import torch num = torch.tensor(135) num = torch.clamp(num, min=10, max=25) print(num.numpy()) Output: 25ConclusionIf the number is in between the range it will hold the value as it isIf the number is lower than the minimum value of the range then it will hold the minimum range valueIf the number is greater than the maximum value of the range then it will hold the maximum range value Comment More infoAdvertise with us Next Article How to clamp floating numbers in Python D deepak2018 Follow Improve Article Tags : Technical Scripter Python Technical Scripter 2022 Practice Tags : python Similar Reads Format a Number Width in Python Formatting numbers to a fixed width is a common requirement in various programming tasks, especially when dealing with data presentation or storage. By understanding these techniques, developers can ensure consistent and visually appealing formatting of numerical data in their Python. Format a Numbe 3 min read How to Round Numbers in Python? Rounding a number simplifies it while keeping its value as close as possible to the original. Python provides various methods to round numbers, depending on how we want to handle the precision or rounding behavior. In this article, we'll cover the most commonly used techniques for rounding numbers i 4 min read How to format numbers as currency strings in Python Formatting numbers as currency strings in Python is a common requirement especially in the applications involving financial data. Formatting numbers as currency strings in Python involves presenting numeric values in the standardized currency format typically including the currency symbol, thousands 3 min read How to convert Float to Int in Python? In Python, you can convert a float to an integer using type conversion. This process changes the data type of a value However, such conversions may be lossy, as the decimal part is often discarded.For example:Converting 2.0 (float) to 2 (int) is safe because here, no data is lost.But converting 3.4 5 min read How to convert fraction to percent in Python? Fractions are a part of a whole commodity that can be expressed in the form of a numerator divided by the denominator. A fraction is of the form \pm\frac{p}{q} , where p<=q. There are various methods of converting between fractions and percentages. Method 1: Manual conversion of the fraction to p 3 min read Like