float default formatting

  • Thread starter =?ISO-8859-15?Q?Bernard_Delm=E9e?=
  • Start date
?

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

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.
 
T

Tim Roberts

Bernard Delmée said:
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.
 
?

=?ISO-8859-1?Q?Bernard_Delm=E9e?=

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.
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top