@@ -363,3 +363,66 @@ def scientific(value, precision=2):
363363 final_str = part1 + " x 10" + "" .join (new_part2 )
364364
365365 return final_str
366+
367+
368+ def clamp (value , format = "{:}" , floor = None , ceil = None , floor_token = "<" , ceil_token = ">" ):
369+ """Returns number with the specified format, clamped between floor and ceil.
370+
371+ If the number is larger than ceil or smaller than floor, then the respective limit
372+ will be returned, formatted and prepended with a token specifying as such.
373+
374+ Examples:
375+ ```pycon
376+ >>> clamp(123.456)
377+ '123.456'
378+ >>> clamp(0.0001, floor=0.01)
379+ '<0.01'
380+ >>> clamp(0.99, format="{:.0%}", ceil=0.99)
381+ '99%'
382+ >>> clamp(0.999, format="{:.0%}", ceil=0.99)
383+ '>99%'
384+ >>> clamp(1, format=intword, floor=1e6, floor_token="under ")
385+ 'under 1.0 million'
386+ >>> clamp(None) is None
387+ True
388+
389+ ```
390+
391+ Args:
392+ value (int, float): Input number.
393+ format (str OR callable): Can either be a formatting string, or a callable
394+ function than receives value and returns a string.
395+ floor (int, float): Smallest value before clamping.
396+ ceil (int, float): Largest value before clamping.
397+ floor_token (str): If value is smaller than floor, token will be prepended
398+ to output.
399+ ceil_token (str): If value is larger than ceil, token will be prepended
400+ to output.
401+
402+ Returns:
403+ str: Formatted number. The output is clamped between the indicated floor and
404+ ceil. If the number if larger than ceil or smaller than floor, the output will
405+ be prepended with a token indicating as such.
406+
407+ """
408+ if value is None :
409+ return None
410+
411+ if floor is not None and value < floor :
412+ value = floor
413+ token = floor_token
414+ elif ceil is not None and value > ceil :
415+ value = ceil
416+ token = ceil_token
417+ else :
418+ token = ""
419+
420+ if isinstance (format , str ):
421+ return token + format .format (value )
422+ elif callable (format ):
423+ return token + format (value )
424+ else :
425+ raise ValueError (
426+ "Invalid format. Must be either a valid formatting string, or a function "
427+ "that accepts value and returns a string."
428+ )
0 commit comments