Aug-20-2018, 05:38 PM
Does anyone know how to trim / clip a float number?
I'm printing out a lot of numbers to my shell window and would like to trim float numbers. I had a quick look at the built in routines but can't remember how to do it. I created a little routine and was wondering if this was the best way to get it done. I'm hoping someone has a better way.
I'm printing out a lot of numbers to my shell window and would like to trim float numbers. I had a quick look at the built in routines but can't remember how to do it. I created a little routine and was wondering if this was the best way to get it done. I'm hoping someone has a better way.
def mytrim(float_number, length =2):
if isinstance(float_number, float):
num =str(float_number)
digits =num.split(".")
return_str =digits[0] +"." +str(digits[1])[:length]
return return_str
# mytrim() ------------------------------------------------------------------
print(mytrim(328.88452), mytrim(31.11547999999999))Output:328.88 31.11
