float default formatting

Tim Roberts timr at probo.com
Sat Mar 26 04:00:39 EST 2005


Bernard Delmée <bdelmee at advalvas.REMOVEME.be> wrote:
>
>Is there a simple way to modify the default sprintf mask
>used for floats ? (eg something like sys.float_mask = '%.2f')
>I've tried assigning to float.__dict__[ '__str__' ], but
>that's apparently not allowed.

No.  The mask string is passed relatively directly to sprintf, with no
hidden charges or destination fees.  %f gets you the whatever your C
library does by default.

This is one way, but there are more efficient methods:

    FLT = "%.2f"
    print ("abc="+FLT+" def="+FLT) % (3.0,4.5)

Are you trying to do currency?  There are better ways, using the locale
methods.
-- 
- Tim Roberts, timr at probo.com
  Providenza & Boekelheide, Inc.



More information about the Python-list mailing list