23

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?

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • Am I missing the point here, or what about the `round()` function: http://docs.python.org/2/library/functions.html#round – TerryA Feb 21 '13 at 08:43
  • Though, I have to say, I'm not a huge fan of solutions that involve `round` simply because I am nervous I'll get `1.1000000000009962` as an output someday. – nneonneo Feb 21 '13 at 08:43
  • 6
    Aarrgh, it's not a duplicate! I don't want trailing `.0`. The other question permits that. – nneonneo Feb 21 '13 at 08:47
  • @nneonneo I believe `int()` will get rid of the `.0`, but use in caution as it will get rid of any other decimal places (so perhaps an if/else statement?) – TerryA Feb 21 '13 at 08:49
  • @Haidro: ...now that's just getting icky. I was hoping I didn't need an `if/else`, or I would just condition on the `'.%2f' % f` or something. (Still weirdly difficult to have a nice solution...I'm really used to things being really easy in Python) – nneonneo Feb 21 '13 at 08:50
  • Och, someone pointed out that `('%.2f' % f).rstrip('.0')` mangles `1000.0` into `1`. So now we need `.rstrip('0').rstrip('.')`... – nneonneo Feb 21 '13 at 11:37

3 Answers3

24

The g formatter limits the output to n significant digits, dropping trailing zeroes:

>>> "{:.3g}".format(1.234)
'1.23'
>>> "{:.3g}".format(1.2)
'1.2'
>>> "{:.3g}".format(1)
'1'
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • 2
    You can use the [`format()` function](http://docs.python.org/2/library/functions.html#format) if all you have is one `{}` template. e.g. `format(f, '.3g')`. This even works correctly for 0. – Martijn Pieters Feb 21 '13 at 11:32
  • 4
    Almost...except that it prints *at most three digits*, rather than *at most two decimal places*. (Example: `'{:.3g}'.format(13.468)` prints `13.5` rather than `13.47`) – nneonneo Feb 21 '13 at 11:34
  • 2
    Ah, and it also does the wrong thing when exponents are involved. – Martijn Pieters Feb 21 '13 at 11:36
  • 2
    OK, I guess you could do `"{0:.{1}g}".format(13.468, 2+len(str(int(13.468))))`, but that's not really Pythonic, I guess :) – Tim Pietzcker Feb 21 '13 at 11:56
21

You need to separate the 0 and the . stripping; that way you won't ever strip away the natural 0.

Alternatively, use the format() function, but that really comes down to the same thing:

format(f, '.2f').rstrip('0').rstrip('.')

Some tests:

>>> def formatted(f): return format(f, '.2f').rstrip('0').rstrip('.')
... 
>>> formatted(0.0)
'0'
>>> formatted(4.797)
'4.8'
>>> formatted(4.001)
'4'
>>> formatted(13.577)
'13.58'
>>> formatted(0.000000000000000000001)
'0'
>>> formatted(10000000000)
'10000000000'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 2
    +1: I guess this is the conclusion I came to independently. I was hoping there was a nicer solution, but there simply might not be one. Thanks! – nneonneo Feb 21 '13 at 11:44
5

In general working with String[s] can be slow. However, this is another solution:

>>> from decimal import Decimal
>>> precision = Decimal('.00')
>>> Decimal('4.001').quantize(precision).normalize()
Decimal('4')
>>> Decimal('4.797').quantize(precision).normalize()
Decimal('4.8')
>>> Decimal('8.992').quantize(precision).normalize()
Decimal('8.99')
>>> Decimal('13.577').quantize(precision).normalize()
Decimal('13.58')

You may find more info here: http://docs.python.org/2/library/decimal.html

Markon
  • 4,480
  • 1
  • 27
  • 39