how to get output in a mixed fraction?

regular rational sage style is like

sage:22/10

i want make it look like (thru pprint it will just look like a mixed on papper)

sage:2+2/10

is this possible?

edit retag close merge delete

Sort by ยป oldest newest most voted

yeah would be great to deal this without that much complex constructions..

got an easier solution btw

sage: a=22/10 sage: floor(a), a - floor(a) (2, 1/5)

more

Note that if a is something like -22/10, that will give (-3, -4/5) which is probably not the desired result.

( 2010-08-21 19:44:36 +0200 )edit

If you want all rational numbers to look like this, then there isn't a convenient way to make that happen in Sage. I'm currently working on a framework to allow one to do this, but it's not ready yet. I will update this post when there is a ticket in question

However, if you just want a function that will return a string in the right form, you can use something like the following

def pprint(rational):
if rational < 0:
return '%s - %s'%(rational.ceil(), rational.ceil() - rational)
elif rational > 0:
return '%s + %s'%(rational.floor(), rational-rational.floor())
else:
return '0'
more

( 2010-08-24 13:08:44 +0200 )edit