Skip to content

Instantly share code, notes, and snippets.

@michalmonday
Created February 6, 2023 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michalmonday/073f24e5f976ab1d386c24e017046a53 to your computer and use it in GitHub Desktop.
Save michalmonday/073f24e5f976ab1d386c24e017046a53 to your computer and use it in GitHub Desktop.
Neat rounding function.
import numpy as np
def float_to_concise_str(f):
''' turns 0.0012345678 into 0.0012 '''
f = float(f)
if len(str(f).split('.')[1]) < 2 or f >= 1.0:
return str(round(f, 2))
return np.format_float_positional( float(f'{f:.2}'), trim='-')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment