how to improve this simple block of code

Steven D'Aprano steve at REMOVETHIScyber.com.au
Fri Jan 13 23:10:51 EST 2006


On Wed, 11 Jan 2006 05:58:05 -0800, py wrote:

> Say I have...
> x = "132.00"
> 
> but I'd like to display it to be "132" ...dropping the trailing
> zeros...I currently try this

Mucking about with the string is one solution. Here is another:

print int(float(x))


> I do it like this because if
> x = "132.15"  ...i dont want to modify it.  But if
> x = "132.60" ...I want it to become "132.6"

Then you want:

x = float("123.60") # full precision floating point value
r = round(x, 1) # rounded to one decimal place



-- 
Steven.




More information about the Python-list mailing list