[SciPy-user] Print number of significant digits ?

Steve Schmerler elcorto at gmx.net
Fri Jun 15 06:17:05 EDT 2007


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'


-- 
cheers,
steve

I love deadlines. I like the whooshing sound they make as they fly by. -- Douglas Adams




More information about the SciPy-User mailing list