float default formatting

Discussion in 'Python' started by =?ISO-8859-15?Q?Bernard_Delm=E9e?=, Mar 24, 2005.

  1. 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.
     
    =?ISO-8859-15?Q?Bernard_Delm=E9e?=, Mar 24, 2005
    #1
    1. Advertisements

  2. =?ISO-8859-15?Q?Bernard_Delm=E9e?=

    Tim Roberts Guest

    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, Mar 26, 2005
    #2
    1. Advertisements

  3. Are you trying to do currency? There are better ways, using the locale
    No, just dumping some data structures and I'd rather not
    check every item's type and format floats explicitly.
    I was hoping the sprintf mask float's __str__ probably
    uses was somehow exposed and could be altered.
     
    =?ISO-8859-1?Q?Bernard_Delm=E9e?=, Mar 26, 2005
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.