Simple string formatting question

  • Thread starter Steven D'Aprano
  • Start date
S

Steven D'Aprano

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?
 
B

Ben Finney

Steven D'Aprano said:
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.
 
F

Fredrik Lundh

Steven said:
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>
 
P

Paul McGuire

Fredrik Lundh said:
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
 
S

Steven D'Aprano

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?
 
B

Ben Finney

Steven D'Aprano said:
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.
 
F

Fredrik Lundh

Paul said:
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>
 
P

Peter Otten

Steven said:
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:
.... 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top