Skip to content

Customize printing of real numbers #7682

@jasongrout

Description

@jasongrout

From http://groups.google.com/group/sage-support/browse_thread/thread/06756df51d828bf4

we probably ought to make this easier to just print the 
first n digits after the decimal by default for RR numbers, or to not 
print out the trailing zeros.  I can't imagine telling my students, for 
example, that they need to do '%.3f'%num every time they come across a 
number, especially since they just want to display the equation, not 
format it as a string.

What do people think about this interface?

sage: RR.print_digits=3
sage: 3.09384
3.094
sage: RR.print_trailing_zeros=False
sage: RR.print_digits=None
sage: 3.09384
3.09384

Make it something like the RR.scientific_notation flag that is currently 
in use.

Additionally, and more flexibly, we could just have something like:


sage: RR.set_print_format('%.3f')
sage: RR(pi)
3.142

or more pythonically

sage: RR.print_format = '%.3f'
sage: RR(pi)
3.142

Edit: the patches below do not allow you to set a C format string or number of digits, but they do provide a framework for setting field-wide printing options, and wrap the current printing options Sage has implemented (plus a few minor extensions, I think).

CC: @robertwb @sagetrac-jkantor @williamstein @kcrisman @egourgoulhon

Component: numerical

Author: Jason Grout

Issue created by migration from https://trac.sagemath.org/ticket/7682

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions