# How to truncate decimal points especially .000000... on floats in an expression in Sage?

I have typed out the following block of code in LaTex/SageTex.

\begin{sagesilent}
var('p1','p2')
q1 = 10 - 10*p1
q2 = 5 - 5*p2
B=1.4*q1+2*q2+0.5*q1*q2
\end{sagesilent}

B is $B=\sage{B}$.


The output is

B is B = 25.0000000000000 (p1 − 1.00000000000000)(p2 − 1)−14.0000000000000p1 − 10p2 + 24.0000000000000.


I would like for it to print out

B is B = 25 (p1 - 1)(p2 - 1)- 14p1 - 10p2 + 24.


I have looked through the Sagemath documentation without much luck. Apparently there is this method n() that takes a digit parameter and returns a truncated version of the expression. However, this returns a truncated version of the entire expression (which has no effect) not the floats that are in it.

• An answer that won't work in general, but will work in your case, is to not use 1.4 and 0.5 but 14/10 and 1/2 instead. Maybe all your examples are really rational so it would be fine to do that? Mar 13, 2015 at 13:00