ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 24 Aug 2010 06:08:44 -0500how to get output in a mixed fraction?http://ask.sagemath.org/question/7583/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?Sat, 21 Aug 2010 07:27:07 -0500http://ask.sagemath.org/question/7583/how-to-get-output-in-a-mixed-fraction/Answer by Mike Hansen for <p>regular rational sage style is like </p>
<p>sage:22/10</p>
<p>i want make it look like (thru pprint it will just look like a mixed on papper)</p>
<p>sage:2+2/10</p>
<p>is this possible?</p>
http://ask.sagemath.org/question/7583/how-to-get-output-in-a-mixed-fraction/?answer=11502#post-id-11502If 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'
Sat, 21 Aug 2010 08:13:36 -0500http://ask.sagemath.org/question/7583/how-to-get-output-in-a-mixed-fraction/?answer=11502#post-id-11502Comment by Jason Grout for <p>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 </p>
<p>However, if you just want a function that will return a string in the right form, you can use something like the following</p>
<pre><code>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'
</code></pre>
http://ask.sagemath.org/question/7583/how-to-get-output-in-a-mixed-fraction/?comment=22912#post-id-22912Can you say a bit more about this framework? Is it a printing framework?Tue, 24 Aug 2010 06:08:44 -0500http://ask.sagemath.org/question/7583/how-to-get-output-in-a-mixed-fraction/?comment=22912#post-id-22912Answer by umren for <p>regular rational sage style is like </p>
<p>sage:22/10</p>
<p>i want make it look like (thru pprint it will just look like a mixed on papper)</p>
<p>sage:2+2/10</p>
<p>is this possible?</p>
http://ask.sagemath.org/question/7583/how-to-get-output-in-a-mixed-fraction/?answer=11476#post-id-11476yeah 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)
Sat, 21 Aug 2010 10:05:32 -0500http://ask.sagemath.org/question/7583/how-to-get-output-in-a-mixed-fraction/?answer=11476#post-id-11476Comment by Mike Hansen for <p>yeah would be great to deal this without that much complex constructions..</p>
<p>got an easier solution btw </p>
<p>sage: a=22/10
sage: floor(a), a - floor(a)
(2, 1/5)</p>
http://ask.sagemath.org/question/7583/how-to-get-output-in-a-mixed-fraction/?comment=22945#post-id-22945Note that if a is something like -22/10, that will give (-3, -4/5) which is probably not the desired result.Sat, 21 Aug 2010 12:44:36 -0500http://ask.sagemath.org/question/7583/how-to-get-output-in-a-mixed-fraction/?comment=22945#post-id-22945