Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #92349 > unrolled thread

Re: Cheat sheet for the new string formatting?

Started byrandom832@fastmail.us
First post2015-06-08 16:52 -0400
Last post2015-06-08 16:52 -0400
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Cheat sheet for the new string formatting? random832@fastmail.us - 2015-06-08 16:52 -0400

#92349 — Re: Cheat sheet for the new string formatting?

Fromrandom832@fastmail.us
Date2015-06-08 16:52 -0400
SubjectRe: Cheat sheet for the new string formatting?
Message-ID<mailman.304.1433796726.13271.python-list@python.org>
On Mon, Jun 8, 2015, at 16:32, Skip Montanaro wrote:
> This is counterintuitive:
> 
> >>> "{:.3}".format(-0.00666762259822)
> '-0.00667'
> >>> "{:.3f}".format(-0.00666762259822)
> '-0.007'
> >>> "%.3f" % -0.00666762259822
> '-0.007'
> >>> "{:.3s}".format(-0.00666762259822)
> ValueError Unknown format code 's' for object of type 'float'
> 
> Why does the first form display five digits after the decimal point?

Because it's three significant figures. Using .3 alone with a float is
equivalent to %.3g, not %.3f

> Why don't floats support "{:.Ns}"? (I know I can use "{!s}".)

Why would they? The old style didn't support %.Ns either. If you want a
width like "%50s" it's {!s:50}. But if you're using !s (or %s with the
old style) you don't get to specify the significant digits, only the
field width, so it's not clear what old-style code you're trying to find
an equivalent to.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web