Skip to content

Instantly share code, notes, and snippets.

@MaHu6
Created December 27, 2018 06:59
Show Gist options
  • Select an option

  • Save MaHu6/517b43d8081cdc3d24dd9b5653e51c89 to your computer and use it in GitHub Desktop.

Select an option

Save MaHu6/517b43d8081cdc3d24dd9b5653e51c89 to your computer and use it in GitHub Desktop.
Keep a few decimal places in python
def keep_decimal(origin_num, places):
num_x, num_y = str(origin_num).split('.')
if len(num_y) > places:
num = float(num_x + '.' + num_y[0:places])
else:
return origin_num
return num
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment