but I don't want to print the trailing ".0"

Gary Herron gherron at islandtraining.com
Thu May 1 19:42:55 EDT 2003


On Thursday 01 May 2003 03:40 pm, Dan Jacobson wrote:
> What is the "in crowd" method to enhance s.py so I don't have to use
> sed to get what I want?
> $ cat s.py
> steps=(1500,1250,1000,625,500)
> for i in steps:
>     print i/100.0 #how do I enhance this line, please? what %f thingy?
> $ python s.py
> 15.0
> 12.5
> 10.0
> 6.25
> 5.0
> $ python s.py|sed 's/\.0$//' #gives what I want
> 15
> 12.5
> 10
> 6.25
> 5


The '%g'  conversion might be what you want.  Here's three examples:

>>> print '%g %g %g' % (123.0, 123.45, 0.00000000000000012345)
123 123.45 1.2345e-16
>>>

Gary Herron







More information about the Python-list mailing list