Created
February 6, 2023 14:16
-
-
Save michalmonday/073f24e5f976ab1d386c24e017046a53 to your computer and use it in GitHub Desktop.
Neat rounding function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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