I want to format a list of floating-point numbers with at most, say, 2 decimal places. But, I don't want trailing zeros, and I don't want trailing decimal points.
So, for example, 4.001
=> 4
, 4.797
=> 4.8
, 8.992
=> 8.99
, 13.577
=> 13.58
.
The simple solution is ('%.2f' % f).rstrip('.0')
('%.2f' % f).rstrip('0').rstrip('.')
. But, that looks rather ugly and seems fragile. Any nicer solutions, maybe with some magical format flags?