By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,923 Members | 1,655 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,923 IT Pros & Developers. It's quick & easy.

float default formatting

P: n/a
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.
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Bernard Delmée <bd*****@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, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 18 '05 #2

P: n/a
> Are you trying to do currency? There are better ways, using the locale
methods.


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.
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.