[SciPy-user] Print number of significant digits ?
Nils Wagner
nwagner@iam.uni-stuttgart...
Fri Jun 15 05:39:04 CDT 2007
Steve Schmerler wrote:
> Stef Mientki wrote:
>
>> Joachim Dahl wrote:
>>
>>> print "%3.2f" %pi
>>>
>> thanks Joachim,
>>
>> but I didn't phrase my question accurate enough,
>> I not only want to print pi, but I want to print anything
>>
>> e.g. I now get:
>> Model 1.0000000149 1.0 [ 1.00000001 1.00000001 3.00000001
>> 4.00000001 5.00000001]
>>
>> but I want
>> Model 1.00 1.0 [ 1.00 1.00 3.00 4.00 5.00]
>>
>> and as I use "print" as a quick and dirty intermediate result for
>> everything,
>> I don't want to spell out each format statement.
>>
>>
>
> Hmm I'm not aware of a built-in for doing this. A quick and dirty solution would be
>
> a = array([1,2,3,pi])
> fmt = "%3.2f "*len(a)
> fmt = fmt.strip()
>
> Then,
>
> In [28]: a
> Out[28]: array([ 1. , 2. , 3. , 3.14159265])
>
> In [29]: fmt %tuple([aa for aa in a])
> Out[29]: '1.00 2.00 3.00 3.14'
>
>
>
You can use set_printoptions
set_printoptions(precision=None, threshold=None, edgeitems=None,
linewidth=None, suppress=None)
Set options associated with printing.
:Parameters:
precision : int
Number of digits of precision for floating point output
(default 8).
threshold : int
Total number of array elements which trigger summarization
rather than full repr (default 1000).
edgeitems : int
Number of array items in summary at beginning and end of
each dimension (default 3).
linewidth : int
The number of characters per line for the purpose of inserting
line breaks (default 75).
suppress : bool
Whether or not suppress printing of small floating point values
using scientific notation (default False).
>>> from scipy import *
>>> set_printoptions(precision=2)
>>> print pi
3.14159265359
>>> print array([pi])
[ 3.14]
Nils
More information about the SciPy-user
mailing list