Re: how to improve this simple block of code

2006-01-13 Thread Steven D'Aprano
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

Re: how to improve this simple block of code

2006-01-12 Thread Tim Williams (gmail)
On 11/01/06, Mike Meyer [EMAIL PROTECTED] wrote: py [EMAIL PROTECTED] writes: Say I have... x = 132.00 but I'd like to display it to be 132 ...dropping the trailing zeros...I currently try this Is it likely that x might not have any decimal places? If so all the above solutions fail when x =130

Re: how to improve this simple block of code

2006-01-11 Thread Juho Schultz
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 if x.endswith(0): x = x[:len(x)-1] if x.endswith(0): x = x[:len(x)-1] if x.endswith(.): x = x[:len(x)-1] I do it like this because if x =

Re: how to improve this simple block of code

2006-01-11 Thread py
hanz wrote: x = x.rstrip('0.') # removes trailing zeroes and dots knew there had to be a way, thanks. -- http://mail.python.org/mailman/listinfo/python-list

Re: how to improve this simple block of code

2006-01-11 Thread Matt Hammond
On Wed, 11 Jan 2006 13:58:05 -, py [EMAIL PROTECTED] wrote: Say I have... x = 132.00 but I'd like to display it to be 132 ...dropping the trailing zeros... How about: if . in x: x, frac = x.split(.) frac = frac.rstrip(0) if frac: x = x + . + frac Copes if x

Re: how to improve this simple block of code

2006-01-11 Thread Mike Meyer
py [EMAIL PROTECTED] writes: Say I have... x = 132.00 but I'd like to display it to be 132 ...dropping the trailing zeros...I currently try this The two-strip solution is cleaner, but: if x.endswith(0): x = x[:len(x)-1] x = x[:-1] or del x[-1] both improve that one

Re: how to improve this simple block of code

2006-01-11 Thread Peter Otten
Mike Meyer wrote: py [EMAIL PROTECTED] writes: Say I have... x = 132.00 but I'd like to display it to be 132 ...dropping the trailing zeros...I currently try this The two-strip solution is cleaner, but: if x.endswith(0): x = x[:len(x)-1] x = x[:-1] or del x[-1]

Re: how to improve this simple block of code

2006-01-11 Thread Gary Duzan
py wrote: x = 132.15 ...i dont want to modify it. But if x = 132.60 ...I want it to become 132.6 is there a better way to do this? It seems a bit ugly to me. The following works as long as you don't mind losing leading zeros as well: x = x.strip('0')

Re: how to improve this simple block of code

2006-01-11 Thread Matt Hammond
How about: if . in x: x, frac = x.split(.) frac = frac.rstrip(0) if frac: x = x + . + frac Or simpler still: if . in x: x = x.rstrip(0) x = x.rstrip(.) More concise, but slightly less readable IMO: if . in x: x =

Re: how to improve this simple block of code

2006-01-11 Thread Peter Hansen
py wrote: hanz wrote: x = x.rstrip('0.') # removes trailing zeroes and dots knew there had to be a way, thanks. But that's not it. :-) This is a wonderful opportunity for you to learn about unit testing, and begin the long process of developing good testing habits. Of all the ideas

Re: how to improve this simple block of code

2006-01-11 Thread Peter Otten
Peter Hansen wrote: Of all the ideas posted, I believe only Mark Hammond's would correctly pass the basic obvious test cases Too bad he didn't post at all :-) Peter -- http://mail.python.org/mailman/listinfo/python-list

Re: how to improve this simple block of code

2006-01-11 Thread Peter Hansen
Peter Otten wrote: Peter Hansen wrote: Of all the ideas posted, I believe only Mark Hammond's would correctly pass the basic obvious test cases Too bad he didn't post at all :-) D'oh! There was a typo in my message above. Naturally, I meant to write M. Hammond instead of Mark Hammond.

Re: how to improve this simple block of code

2006-01-11 Thread Mel Wilson
py wrote: Say I have... x = 132.00 but I'd like to display it to be 132 ...dropping the trailing zeros... print '%g' % (float(x),) might work. Mel. -- http://mail.python.org/mailman/listinfo/python-list

RE: how to improve this simple block of code

2006-01-11 Thread Ron Griswold
How 'bout: X = 132.00; Y = int(float(X)); Ron Griswold Character TD R!OT Pictures [EMAIL PROTECTED] -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Mel Wilson Sent: Wednesday, January 11, 2006 1:08 PM To: python-list@python.org Subject: Re: how to

Re: how to improve this simple block of code

2006-01-11 Thread Xavier Morel
Mel Wilson wrote: py wrote: Say I have... x = 132.00 but I'd like to display it to be 132 ...dropping the trailing zeros... print '%g' % (float(x),) might work. Mel. The input is a string, %g expects a float, TypeError exception. --

Re: how to improve this simple block of code

2006-01-11 Thread Xavier Morel
Forget about the previous mail, i just saw you were converting the string to float beforehand, in which case he would more than likely run into the good ol' float imprecision issue sooner than later. Not to mention that %g formats to scientific notation (e.g. exponential format with the

Re: how to improve this simple block of code

2006-01-11 Thread Peter Hansen
Ron Griswold wrote: How 'bout: X = 132.00; Y = int(float(X)); This fails on anything that isn't already an integer (in the math sense, not the Python type sense), such as 132.15 which was one of the OP's actual examples. Matt's answer is still the only one that passes the tests. Mel's is

Re: how to improve this simple block of code (str.partition?)

2006-01-11 Thread David Murmann
Peter Hansen schrieb: Matt's answer is still the only one that passes the tests. well, here's another one: - def mysplit(s, sep): x = s.rsplit(sep, 1) return x + ['']*(2-len(x)) def stripZeros(x): intpart, frac = mysplit(x, '.') frac =