# 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.

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.

add a comment

3

I worked on a ticket to help with this a while ago: http://trac.sagemath.org/sage_trac/ti...

You can see a comment 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*x
```

1

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.

1

I 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.6667
```

0

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.

Yes, 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?

I 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.

Asked: **
2012-11-08 07:57:18 -0500
**

Seen: **771 times**

Last updated: **Mar 10 '15**

Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.