Most Pythonic way to print *at most* some number of decimal places [duplicate]
问题 This question already has answers here : Formatting floats in Python without trailing zeros (15 answers) Closed 5 years ago . 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