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.Wed, 11 Mar 2015 06:17:53 -0500formatting numbers in sagetexhttp://ask.sagemath.org/question/9515/formatting-numbers-in-sagetex/If I put (for example) \sage{1.2*V} in my .tex file the resulting pdf has
1.20000000000000 V
How can I tell it to not print trailing zeros?
Thanks.Thu, 08 Nov 2012 07:57:18 -0600http://ask.sagemath.org/question/9515/formatting-numbers-in-sagetex/Answer by John Palmieri for <p>If I put (for example) \sage{1.2*V} in my .tex file the resulting pdf has</p>
<p>1.20000000000000 V</p>
<p>How can I tell it to not print trailing zeros?</p>
<p>Thanks.</p>
http://ask.sagemath.org/question/9515/formatting-numbers-in-sagetex/?answer=18732#post-id-18732Depending on the type of calculations, you might be able to work in RDF, for example, or in `RealField(23)`, replacing 23 by whatever number gives you the desired precision.Fri, 09 Nov 2012 06:37:01 -0600http://ask.sagemath.org/question/9515/formatting-numbers-in-sagetex/?answer=18732#post-id-18732Comment by slelievre for <p>Depending on the type of calculations, you might be able to work in RDF, for example, or in `RealField(23)`, replacing 23 by whatever number gives you the desired precision.</p>http://ask.sagemath.org/question/9515/formatting-numbers-in-sagetex/?comment=26151#post-id-26151In addition to the nicer output without trailing zeros, using RDF will usually make everything faster.Wed, 11 Mar 2015 06:17:53 -0500http://ask.sagemath.org/question/9515/formatting-numbers-in-sagetex/?comment=26151#post-id-26151Answer by Dirk Danckaert for <p>If I put (for example) \sage{1.2*V} in my .tex file the resulting pdf has</p>
<p>1.20000000000000 V</p>
<p>How can I tell it to not print trailing zeros?</p>
<p>Thanks.</p>
http://ask.sagemath.org/question/9515/formatting-numbers-in-sagetex/?answer=14200#post-id-14200Try \sage{(1.2).n(6)*V}. The .n(6) command specifies the number of significant bits used to represent the number. As a rule of thumb 3 bits ~ 1 digit.Thu, 08 Nov 2012 09:36:06 -0600http://ask.sagemath.org/question/9515/formatting-numbers-in-sagetex/?answer=14200#post-id-14200Comment by naegling for <p>Try \sage{(1.2).n(6)*V}. The .n(6) command specifies the number of significant bits used to represent the number. As a rule of thumb 3 bits ~ 1 digit.</p>
http://ask.sagemath.org/question/9515/formatting-numbers-in-sagetex/?comment=18738#post-id-18738Yes, that would work for this example, but what in the general case \sage{x} where x is the result of some calculation I can't just call .n(6) on the result because there are non-numeric variables. Does this mean that I should call .n on all the floating point numbers as they enter into the calculation?Thu, 08 Nov 2012 10:29:21 -0600http://ask.sagemath.org/question/9515/formatting-numbers-in-sagetex/?comment=18738#post-id-18738Comment by Dirk Danckaert for <p>Try \sage{(1.2).n(6)*V}. The .n(6) command specifies the number of significant bits used to represent the number. As a rule of thumb 3 bits ~ 1 digit.</p>
http://ask.sagemath.org/question/9515/formatting-numbers-in-sagetex/?comment=18733#post-id-18733I think so, yes. In general you know from the start if the result will be numeric or not. And most of the time you also know the precision you are aiming at. Fri, 09 Nov 2012 00:05:25 -0600http://ask.sagemath.org/question/9515/formatting-numbers-in-sagetex/?comment=18733#post-id-18733Answer by Kevin Cox for <p>If I put (for example) \sage{1.2*V} in my .tex file the resulting pdf has</p>
<p>1.20000000000000 V</p>
<p>How can I tell it to not print trailing zeros?</p>
<p>Thanks.</p>
http://ask.sagemath.org/question/9515/formatting-numbers-in-sagetex/?answer=26143#post-id-26143I find that the easiest way to to convert to a python floating point before printing.
float(24/5) #-> 4.8
"%.4f" % float(26/3) #-> 8.6667Tue, 10 Mar 2015 17:50:08 -0500http://ask.sagemath.org/question/9515/formatting-numbers-in-sagetex/?answer=26143#post-id-26143Answer by Jason Grout for <p>If I put (for example) \sage{1.2*V} in my .tex file the resulting pdf has</p>
<p>1.20000000000000 V</p>
<p>How can I tell it to not print trailing zeros?</p>
<p>Thanks.</p>
http://ask.sagemath.org/question/9515/formatting-numbers-in-sagetex/?answer=14243#post-id-14243I worked on a ticket to help with this a while ago: http://trac.sagemath.org/sage_trac/ticket/7682
You can see a [comment](http://trac.sagemath.org/sage_trac/ticket/7682#comment:32) from Carl Witty that gives a good approach: (I really like his option 1). Basically, it allows for setting options for printing any numbers from a RealField.
For an individual number, you're looking for the skip_zeroes argument to str:
sage: a=1.2
sage: a
1.20000000000000
sage: a.str(skip_zeroes=True)
'1.2'
Alternatively, this is the default behavior for python floats (as opposed to Sage floating point numbers), so if you either turn off the preparser or use python floats, you'll get what you want:
sage: float(1.2)*x
1.2*x
sage: preparser(False)
sage: 1.2*x
1.2*xFri, 09 Nov 2012 04:04:53 -0600http://ask.sagemath.org/question/9515/formatting-numbers-in-sagetex/?answer=14243#post-id-14243