Skip to content

Instantly share code, notes, and snippets.

@yanivk1984
Created March 5, 2022 11:44
Show Gist options
  • Select an option

  • Save yanivk1984/e8f75f4e231c50123c2fa51b211d3190 to your computer and use it in GitHub Desktop.

Select an option

Save yanivk1984/e8f75f4e231c50123c2fa51b211d3190 to your computer and use it in GitHub Desktop.
def check_float_or_int(value: [int, float], return_str=False) -> [str, float, int]:
"""
The function will take a a number and see if it can be int without changing its value.
for example for 1.0 the function will change the type from float to int and return 1.
If the number is a float of 1.2 the program will return float 1.2.
:param value: number, can be int float or str
:param return_str: if this flag set to True, the number will be returned as a string
:return: the number itself, if its a float whole number it will convert to int.
"""
int_ = int(value)
float_ = float(value)
if int_ == float_:
value = int_
else:
value = round(float_, 1)
if return_str:
return str(value)
return value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment