2

Is there any way to truncate numbers inside dollar signs? I found a solution using siunitx and \num{somenumber}, but I have a very large output with dollar signs. To make things worse, some of the numbers are in a format like this: $\phantom{000}0.024$ so I can't even replace the dollar signs with \num (I think phantom doesn't work well with \num). So is there any workaround, to truncate the presented number inside dollar signs, to 2 digits after decimal for example? For example: 2.5678 -> 2.56

  • 2
    By "round", do you mean to the closest integer? Also, just to make sure, you mean "round" and not "truncate"? – Steven B. Segletes Nov 8 '17 at 10:36
  • I actually meant truncate. Sorry, my English is not my first language. For example 2.5678 will be 2.57. Will edit the main post. – Nashh Nov 8 '17 at 10:41
  • Truncating a number actually means making 2.56 out of a 2.5678. If the \phantom{000} is there for alignment purposes, a S column provided by siunitx would allow aligning numbers on the decimal marker inside a tabular. – lblb Nov 8 '17 at 10:46
  • Thanks for the correction about truncating. It doesn't matter to me that much if it is 2.56 or 2.57, but just to have fewer digits. I wasn't aware of the purpose of phantom, it is something that I have in my output whether I like it or not. So I guess I can remove all of them, but I still wonder if there is a way to truncate dollar signs. – Nashh Nov 8 '17 at 10:51
4

As long as the \phantoms are to the left of the decimal...

\documentclass{article}
\newenvironment{specialmath}{\catcode`$=\active}{}
{\catcode`$=\active
\gdef$#1${\dollar#1.000\enddollar}
\gdef\dollar#1.#2#3#4\enddollar{\ensuremath{#1.\ifx.#200\else#2\ifx.#30\else#3\fi\fi}}
}
\begin{document}
\begin{specialmath}
$2$\par
$2.$\par
$2.3$\par
$2.35$\par
$2.357$\par
$2.3578$\par
$52.3578$\par
$\phantom{000}52.3578$
\end{specialmath}

The dollar is restored $x^2$
\end{document}

enter image description here

At the OP's request, here is a version that will not add a decimal if there is none in the originally presented number:

\documentclass{article}
\newenvironment{specialmath}{\catcode`$=\active}{}
{\catcode`$=\active
\gdef$#1${\dollar#1.\relax\relax\enddollar}
\gdef\dollar#1.#2#3#4\enddollar{\ensuremath{%
  #1\ifx\relax#2\else%
    \ifx.#2.00\else.#2\ifx.#30\else#3\fi\fi%
  \fi%
}}
}
\begin{document}
\begin{specialmath}
$2$\par
$2.$\par
$2.3$\par
$2.35$\par
$2.357$\par
$2.3578$\par
$52.3578$\par
$\phantom{000}52.3578$
\end{specialmath}

The dollar is restored $x^2$
\end{document}

enter image description here

  • That's great. Is there a way to keep integers without a decimal? Meaning 2, will stay 2, and not 2.00. – Nashh Nov 8 '17 at 11:00
  • 1
    @Nashh I have edited my answer to add that solution, as well. – Steven B. Segletes Nov 8 '17 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.