# can i display only two decimals of precision

I have some lists I want to display. I don't want to limit the precision for the arithmetic that created them, I only want to limit the number of decimal places (not significant figures) that are displayed.

def f(n): return sorted([ 2*cos(2*3.14159*j/n) for j in range(n) ]) ;
f(5)


Gives me the five required values displayed with what seems to be 14 digits after the decimal. Is it possible to change 14 to some other value?

edit retag close merge delete

Sort by » oldest newest most voted

Given most object in Sage that have a numerical counterpart you can use the function numerical_approx.

sage: pi.numerical_approx(digits=2)
3.1
sage: (cos(pi/7) + sin(pi/13)).numerical_approx(digits=10)
1.140284532


Alternatively, you obtain the same result with

sage: numerical_approx(pi, digits=2)
3.1
sage: numerical_approx(cos(pi/7) + sin(pi/13), digits=10)
1.140284532

more

Note that a predefined abbreviation of numerical_approx is n:

sage: n(pi, digits=2)
3.1

( 2019-01-29 04:07:56 -0500 )edit

This gives 2 significant digits. Is it possible to get two digits after the decimal? So I'd like pi to display as 3.14 and I'd like 10*pi to display as 31.42.

( 2019-01-29 16:02:42 -0500 )edit

Then ask for 4 digits. What do you want to do with 0.0032? You want to display 0.00? You can figure out how many digits you actually want to see by computing the logarithm in base 10 of the absolute value of your number (= number of digits of the integer part).

( 2019-01-30 03:24:51 -0500 )edit

Actually, yes, I want 0.0032 to display as 0.00 (not always of course, but in this case).

I have a bunch of values, and I want to see them all rounded off to the same decimal digit. They have different magnitudes, so asking for a number of significant digits is not the same thing. I am only talking about displaying them, not about actually rounding off the values for further calculation.

Yes I can compute the number of significant digits required for every number I wish to display, and then display it with that number of significant digits, but I was hoping for osme way of saying "Hey, Sagemath, can you please just display everything to two decimals for the moment!" :-)

( 2019-02-13 03:57:45 -0500 )edit

Just use the usual pythonic way to format numbers. For instance:

myList = (pi, exp(16), sqrt(2019))
for x in myList:
print "%.2f" % x


The above gives:

3.14
8886110.52
44.93


(Using format there are some problems, but the above works and it is enough for our purposes.) So in our case, in a dialog with the sage interpreter:

sage: myList = sorted([ 2*cos(2*pi*j/5) for j in range(5) ])
sage: myList
[-1/2*sqrt(5) - 1/2,
-1/2*sqrt(5) - 1/2,
1/2*sqrt(5) - 1/2,
1/2*sqrt(5) - 1/2,
2]
sage: print [ '%.2f' % x for x in myList ]
['-1.62', '-1.62', '0.62', '0.62', '2.00']
sage: print ', '.join( [ '%.2f' % x for x in myList ] )
-1.62, -1.62, 0.62, 0.62, 2.00

more

This accomplishes what I want; is there a way to apply this to displaying the entries of a matrix?

Ideally I would like to be able to set the "default" method for displaying floating point. At the moment the default (at least on my machine) seems to be to display every floating point number to 15 significant digits.

( 2019-02-13 04:06:04 -0500 )edit