%g and fpformat.sci()
Sivakumar Bhaskarapanditha
Sivakumar.Bhaskarapanditha at abaqus.com
Thu Jun 30 14:21:46 EDT 2005
Hi,
How can I control the number of digits after the decimal point using the %g
format specifier. I am not able to get more than 3 digits as shown below.
>>> a = 1.234e-5
>>> '%g' % a
'1.234e-005'
>>> '%.2g' % a
'1.2e-005'
>>> '%.4g' % a
'1.234e-005'
>>> '%.5g' % a
'1.234e-005'
>>> '%.6g' % a
'1.234e-005'
>>> '%.7g' % a
'1.234e-005'
>>> import fpformat
>>> fpformat.sci(a,7)
'1.2340000e-005'
>>>
In the above example there are only 3 digits after decimal. If I set
a=1.234567e-8 I can control part of it but I am trying to find a way of
filling the rest with zeros as given by the fpformat.
Ofcourse I can use the fpformat or do some checks. But is there any direct
way (I need a something that doesn't affect performance).
Thanks a bunch for your help.
Regards,
Siva
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20050630/2785720b/attachment.html>
More information about the Python-list
mailing list