[SciPy-user] nice floating point display ?
Robert Kern
robert.kern at gmail.com
Thu Feb 28 19:07:04 EST 2008
On Thu, Feb 28, 2008 at 5:43 PM, Stef Mientki <s.mientki at ru.nl> wrote:
> hello,
>
> I'm creating an float / log slider,
> now I need a nice representation of the min / max / value
> of the slider.
>
> For some unknown reason
> set_printoptions(precision=2)
> doesn't work,
> and I doubt if it is adequate in my application,
> because precision and notation depends more on the range
> than on the actual value itself.
>
> Does anyone has a elegant solution ?
set_printoptions() doesn't affect Python floats at all, and that looks
like what you are trying to print.
You can explicitly format the numbers yourself:
http://docs.python.org/dev/library/stdtypes.html#string-formatting-operations
In [39]: x = 1.2345678901
In [40]: '%f' % x
Out[40]: '1.234568'
In [41]: '%g' % x
Out[41]: '1.23457'
In [42]: '%1.2f' % x
Out[42]: '1.23'
In [43]: '%1.10f' % x
Out[43]: '1.2345678901'
In [46]: '%f' % (x*1e20)
Out[46]: '123456789009999986688.000000'
In [47]: '%g' % (x*1e20)
Out[47]: '1.23457e+20'
In [48]: '%e' % x
Out[48]: '1.234568e+00'
In [56]: '%.*f' % (3, x)
Out[56]: '1.235'
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the SciPy-User
mailing list