[Tex/LaTex] Formatting Decimals

formatting

Suppose I have a long decimal (assume it is ~6 inches long when written out in 12 point font in LaTeX when I center it, so something like

0.123719283791283718927489749875289345793485723495234572935239452348952384573481234879

How would I make it look like

0.123 719 283 791 283 718 
  927 489 749 875 289 345 
  793 485 723 495 234 572 
  935 239 452 348 952 384 
  573 481 234 879

or some other way that makes the decimal look more visually appealing? Is there a package I could use?

Best Answer

Expanding on Jake's answer, here is a method for deciding also how many three digit groups one wants:

\documentclass[a4paper]{article}
\usepackage[autolanguage]{numprint}
\usepackage{calc}

\newlength{\ipwd}

\newcommand{\printlongdecimal}[2][4]{%
  \begingroup\npthousandsep{\hspace{0.3em}}
  \calcintegerpartwd{#2}%
  \minipage{\ipwd+\widthof{000}*#1+0.3em*(#1-1)}
    \hangindent=\ipwd \hangafter=1
    \numprint{#2}
    \endminipage\endgroup}
\newcommand\calcintegerpartwd[1]{%
  \setlength{\ipwd}{\widthof{\numprint{\splitnumber#1.\relax}.}}}
\def\splitnumber#1.#2\relax{#1}

\begin{document}
\printlongdecimal{1.22344344555000011}
\printlongdecimal[3]{1.22344344555000011}
\printlongdecimal[2]{1.22344344555000011}
\printlongdecimal[2]{1234.22344344555000011}
\printlongdecimal[2]{12345.22344344555000011}
\end{document}

The optional argument to \printlongdecimal is the number of three digit groups one wants in each row.

enter image description here

Related Question