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
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
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 =
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
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
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
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]
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')
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 =
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
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
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.
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
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
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.
--
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
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
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 =
18 matches
Mail list logo