Regular Expression - old regex module vs. re module

Paul McGuire ptmcg at austin.rr._bogus_.com
Fri Jun 30 20:59:14 CEST 2006


"Jim Segrave" <jes at nl.demon.net> wrote in message
news:12aaigaohtou291 at corp.supernews.com...
> If fails for floats specified as ###. or .###, it outputs an integer
> format and the decimal point separately. It also ignores \# which
> should prevent the '#' from being included in a format.
>

Here's a little more study on this (all tests are using Python 2.4.1):

If floats are specified as "###.", should we generate "%4.0f" as the result?
In fact, to get 3 leading places and a trailing decimal point, when 0
decimal places are desired, should be formatted with "%3.0f." - we have to
explicitly put in the trailing '.' character.
>>> print ">%1.0f<" % 10.00001
>10<
>>> print ">%2.0f<" % 10.00001
>10<
>>> print ">%3.0f<" % 10.00001
> 10<
>>> print ">%3.0f.<" % 10.00001
> 10.<
But as we see below, if the precision field is not zero, the initial width
consumes one character for the decimal point.  If the precision field *is*
zero, then the entire width is used for the integer part of the value, with
no trailing decimal point.

".###" almost makes no sense.  There is no floating point format that
suppresses the leading '0' before the decimal point.
>>> print ">%1.2f<" % 0.00001
>0.00<
>>> print ">%2.2f<" % 0.00001
>0.00<
>>> print ">%3.2f<" % 0.00001
>0.00<
>>> print ">%4.2f<" % 0.00001
>0.00<
>>> print ">%5.2f<" % 0.00001
> 0.00<

Using the %f with a nonzero precision field, will always output at least the
number of decimal places, plus the decimal point and leading '0' if number
is less than 1.

This whole discussion so far has also ignore negative values, again, we
should really look more into the spec for this formatting scheme, rather
than try to read the OP's mind.

-- Paul





More information about the Python-list mailing list