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  ...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.

-- 
http://mail.python.org/mailman/listinfo/python-list


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 except Matt Hammond's offering which splits x at the decimal point
if one exists.

-- 
http://mail.python.org/mailman/listinfo/python-list

how to improve this simple block of code

2006-01-11 Thread py
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 = 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.

T.I.A
(thanks in advance)

-- 
http://mail.python.org/mailman/listinfo/python-list


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 = 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.
 
 T.I.A
 (thanks in advance)
 

x = x.rstrip('0') # removes trailing zeros
x = x.rstrip('.') # removes trailing dot(s)
-- 
http://mail.python.org/mailman/listinfo/python-list


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 = 132 too. If there'll always be a decimal point, then you  
can leave off the initial if.



Matt
-- 

| Matt Hammond
| RD Engineer, BBC Research  Development, Tadworth, Surrey, UK.
| http://kamaelia.sf.net/
| http://www.bbc.co.uk/rd/
-- 
http://mail.python.org/mailman/listinfo/python-list


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 statement.

  mike
-- 
Mike Meyer [EMAIL PROTECTED]  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


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]
 
 both improve that one statement.

 del it's tempting not to try[-1]
Traceback (most recent call last):
  File stdin, line 1, in ?
TypeError: object doesn't support item deletion

Just-pointing-out-what-does-not-work-ly yours

Peter
-- 
http://mail.python.org/mailman/listinfo/python-list


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')

Gary Duzan
Motorola CHS
-- 
http://mail.python.org/mailman/listinfo/python-list


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 = x.rstrip(0).rstrip(.)

-- 

| Matt Hammond
| RD Engineer, BBC Research  Development, Tadworth, Surrey, UK.
| http://kamaelia.sf.net/
| http://www.bbc.co.uk/rd/
-- 
http://mail.python.org/mailman/listinfo/python-list


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 posted, I believe only Mark Hammond's would correctly pass the 
basic obvious test cases, and I don't think anyone (without actually 
having checked with tests) should be saying even his is clearly correct.

import unittest
from yourmodule import stripZeros  # or whatever you have

class Tests(unittest.TestCase):
 def test01(self):
 'check zero-stripper'
 for input, expected in [
 ('', ''),
 ('0', '0'),
 ('0.0', '0'),
 ('0.', '0'),
 ('000.', '000'),
 ('10', '10'),
 ('10.0', '10'),
 ('foo', 'foo'),
 ('foo.', 'foo'), # ??
 ('132.15', '132.15'),
 ('132.60', '132.6'),
 ('132.00', '132'),
 ('132.0', '132'),
 ('132.01', '132.01'),
 # add others to taste
 ]:
 self.assertEquals(expected, stripZeros(input))

unittest.main()


Change the above test cases to match what you really want if they're not 
correct, then run the script and make sure everything passes.

-Peter

-- 
http://mail.python.org/mailman/listinfo/python-list


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.  ;-)

Sorry Matt!

-Peter

-- 
http://mail.python.org/mailman/listinfo/python-list


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 improve this simple block of code

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

-- 
http://mail.python.org/mailman/listinfo/python-list


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.
-- 
http://mail.python.org/mailman/listinfo/python-list


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 exponent always being a multiple of 3), he'd probably 
use %f.
-- 
http://mail.python.org/mailman/listinfo/python-list


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 
correct provided the OP's input data is limited in several ways, which 
it may well be.  (For example, the '%g' approach fails if the input has 
too many decimal places, such as 132.0001 or 1234567.  It also can't 
handle non-numbers or empty input, though the OP may not care about any 
of these cases.)

All this goes to show there are a *lot* of ways to write code that 
doesn't work, but apparently only one way to be sure it works.

--
Peter

-- 
http://mail.python.org/mailman/listinfo/python-list


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 = frac.rstrip('0')
if frac:
return intpart+'.'+frac
else:
return intpart
-

i only mention this one, because i just remembered the discussion
about a str.partition method, which could save this version the
extra function. what happened to that method?

for anyone interested, it was proposed here:

  http://mail.python.org/pipermail/python-dev/2005-August/055764.html

--
David.
-- 
http://mail.python.org/mailman/listinfo/python-list