I'm trying to round floor to int or to 1 decimal for non-zero float with letters k, M, B....
def human_format(num):
num = float(f'{num:.3g}')
magnitude = 0
while abs(num) >= 1000:
magnitude += 1
num /= 1000.0
num = int(num * 10) / 10
return f"{f'{num:f}'.rstrip('0').rstrip('.')}{['', 'k', 'M', 'B', 'T'][magnitude]}"
Now this function returns as expected in this case:
human_format(1294)
'1.2k'
And rounds up in the case below:
human_format(1295)
'1.3k'
The rounding happens due to the flag g in string format and I don't know how to tune it.
How can I avoid rounding up, keeping everything else the same?