469,271 Members | 1,009 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Simple string formatting question

I have a sinking feeling I'm missing something really,
really simple.

I'm looking for a format string similar to '%.3f'
except that trailing zeroes are not included.

To give some examples:

Float String
1.0 1
1.1 1.1
12.1234 12.123
12.0001 12

and similar.

Here is a (quick and dirty) reference implementation:

def format(f, width=3):
fs = '%%.%df' % width
s = fs % f
return s.rstrip('0').rstrip('.')
Is there a way of getting the same result with just a
single string format expression?

--
Steven

Apr 6 '06 #1
7 1458
"Steven D'Aprano" <st***@REMOVEMEcyber.com.au> writes:
I have a sinking feeling I'm missing something really, really
simple.
"Oh no, everyone in the galaxy gets that, that's perfectly natural
paranoia."
I'm looking for a format string similar to '%.3f' except that
trailing zeroes are not included.


Can;t be done, to my knowledge. You specify a particular precision,
and the number will be formatted at that precision. This matches the
'printf' behaviour that inspired string formatting operations.

If your brute-force approach works, stick to that until you have a
reason to look for something better.

--
\ "I have an answering machine in my car. It says, 'I'm home now. |
`\ But leave a message and I'll call when I'm out.'" -- Steven |
_o__) Wright |
Ben Finney

Apr 6 '06 #2
Steven D'Aprano wrote:
Here is a (quick and dirty) reference implementation:

def format(f, width=3):
fs = '%%.%df' % width
s = fs % f
return s.rstrip('0').rstrip('.')

Is there a way of getting the same result with just a
single string format expression?


not with % itself, but you can do it all in one line:

def format(f, width=3): return ("%.*f" % (width, f)).rstrip(".0")

</F>

Apr 6 '06 #3
"Fredrik Lundh" <fr*****@pythonware.com> wrote in message
news:ma***************************************@pyt hon.org...
Steven D'Aprano wrote:
Here is a (quick and dirty) reference implementation:

def format(f, width=3):
fs = '%%.%df' % width
s = fs % f
return s.rstrip('0').rstrip('.')

Is there a way of getting the same result with just a
single string format expression?


not with % itself, but you can do it all in one line:

def format(f, width=3): return ("%.*f" % (width, f)).rstrip(".0")

</F>

Ooops, don't combine the two calls to rstrip().

def format(f, width=3): return ("%.*f" % (width, f)).rstrip(".0")

print format(3.140)
print format(3.000)
print format(3.0000001)
print format(30.0000)
print format(300000.000)

Gives:
3.14
3
3
3
3

But this works fine:
def format(f, width=3): return ("%.*f" % (width, f)).rstrip("0").rstrip(".")
-- Paul


Apr 6 '06 #4
On Thu, 06 Apr 2006 22:16:05 +1000, Ben Finney wrote:
"Steven D'Aprano" <st***@REMOVEMEcyber.com.au> writes:

I'm looking for a format string similar to '%.3f' except that
trailing zeroes are not included.


Can;t be done, to my knowledge. You specify a particular precision,
and the number will be formatted at that precision. This matches the
'printf' behaviour that inspired string formatting operations.

If your brute-force approach works, stick to that until you have a
reason to look for something better.


Thanks to everybody who replied, I'm glad to see I didn't miss anything.

I do actually have a reason for wanting a single format pattern, but I can
probably work with what I have, it will just be less convenient.

I hesitate to suggest this, what with the whole PEP procedure and all
being so intimidating to those who aren't familiar with it (e.g. me), but
would there be any interest in extending the formatting operations to
include such a feature? It would (presumably) break compatibility with
printf, but does that matter?

--
Steven.

Apr 6 '06 #5
"Steven D'Aprano" <st***@REMOVETHIScyber.com.au> writes:
On Thu, 06 Apr 2006 22:16:05 +1000, Ben Finney wrote:
"Steven D'Aprano" <st***@REMOVEMEcyber.com.au> writes: 1> >> I'm looking for a format string similar to '%.3f' except that
trailing zeroes are not included.


Can;t be done, to my knowledge. You specify a particular
precision, and the number will be formatted at that
precision. This matches the 'printf' behaviour that inspired
string formatting operations.


[...] would there be any interest in extending the formatting
operations to include such a feature? It would (presumably) break
compatibility with printf, but does that matter?


-1 from me. The concept of "precision" is fairly well defined; you're
talking about combining that with something more special-case. In
general, "special cases aren't special enough to break the rules", and
the programemr should be forced to spell out what he's doing so that
it's obvious.

In the specific cases of the string formatting codes, they're a
special case that is a marginal win because of the large body of
existing experience with the standard C printf codes. That's weighed
against their considerable lack of readability; expand them to be
special for Python, and that "marginal win" is gone.

--
\ "My girlfriend has a queen sized bed; I have a court jester |
`\ sized bed. It's red and green and has bells on it, and the ends |
_o__) curl up." -- Steven Wright |
Ben Finney

Apr 7 '06 #6
Paul McGuire wrote:
Ooops, don't combine the two calls to rstrip().

def format(f, width=3): return ("%.*f" % (width, f)).rstrip(".0")

print format(3.140)
print format(3.000)
print format(3.0000001)
print format(30.0000)
print format(300000.000)
hey, I'm doing test-driven development. being able to handle 30 and
300000 wasn't part of the original spec ;-)
But this works fine:
def format(f, width=3): return ("%.*f" % (width, f)).rstrip("0").rstrip(".")


indeed.

</F>

Apr 7 '06 #7
Steven D'Aprano wrote:
I have a sinking feeling I'm missing something really,
really simple.

I'm looking for a format string similar to '%.3f'
except that trailing zeroes are not included.

To give some examples:

Float String
1.0 1
1.1 1.1
12.1234 12.123
12.0001 12

and similar.

Here is a (quick and dirty) reference implementation:

def format(f, width=3):
fs = '%%.%df' % width
s = fs % f
return s.rstrip('0').rstrip('.')
Is there a way of getting the same result with just a
single string format expression?


Does it have to be a string or can you cheat? If so:
class Format(str): .... def __new__(cls, width):
.... return str.__new__(cls, "%%.%df" % width)
.... def __mod__(self, other):
.... return str.__mod__(self, other).rstrip("0").rstrip(".")
.... format = Format(3)
for f in [1.0, 1.1, 12.1234, 12.0001]:

.... print f, "-->", format % f
....
1.0 --> 1
1.1 --> 1.1
12.1234 --> 12.123
12.0001 --> 12

For passing around in your app that should be as convenient as a string --
of course it will break if you store the "format" in a file, say.

Peter
Apr 7 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Ot | last post: by
8 posts views Thread by Tony | last post: by
3 posts views Thread by The Eeediot | last post: by
6 posts views Thread by alexrussell101 | last post: by
8 posts views Thread by rdrink | last post: by
11 posts views Thread by Dustan | last post: by
2 posts views Thread by RichB | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.