# Formatting Decimals

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?

-
Out of sheer curiosity: Is there an actual application that requires reporting a number to 84 significant figures? – Jake Sep 14 '11 at 4:23
@Jake: See page 6 of this pdf which has 2000 decimal places to use in an unstable recursion. Page 8 has 200 decimal places of another number. In both cases there are 50 digits per line. – Henry Sep 14 '11 at 10:40
@Jake: Computation of the most possible digits of π for instance by far exceeds 84 digits: π. Quoting from Wikipedia: "One open question about π is whether it is a normal number—whether any digit block occurs in the expansion of π just as often as one would statistically expect if the digits had been produced completely 'randomly', and that this is true in every integer base, not just base 10." It even would seem the succession of digits is best described using chaos theory. – Count Zero Sep 14 '11 at 22:50

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.

-

Based on Peter Grill's answer, if you want the groups of digits to be aligned similarly the example in your question, you could switch off the indenting for the paragraph containing the number, pull back the first line by the width of the characters 0., and increase the left margin by the same amount. Wrapped into a macro, this could look like

\newlength{\widthofzeroandperiod}
\settowidth{\widthofzeroandperiod}{0.}

\newcommand{\printlongdecimal}[1]{
\raggedright%
\noindent%
\hspace{-\widthofzeroandperiod}%
\leftskip\widthofzeroandperiod%
\rightskip0.7\textwidth%
\numprint{#1}
}

Using it in an example:

In the following paragraph, you can see an absurdly long number, aligned in a pretty but meaningless way:

\printlongdecimal{0.123719283791283718927489749875289345793485723495234572935239452348952384573481234879}

will yield

Here's the full document:

\documentclass{article}
\usepackage{numprint}
\usepackage{bigintcalc}
\npdecimalsign{\ensuremath{.}}
\npthousandsep{\hspace{0.3em}}

\newlength{\widthofzeroandperiod}
\settowidth{\widthofzeroandperiod}{0.}

\newcommand{\printlongdecimal}[1]{
\raggedright%
\noindent%
\hspace{-\widthofzeroandperiod}%
\leftskip\widthofzeroandperiod%
\rightskip0.7\textwidth%
\numprint{#1}
}

\begin{document}
In the following paragraph, you can see an absurdly long number, aligned in a pretty but meaningless way:

\printlongdecimal{0.123719283791283718927489749875289345793485723495234572935239452348952384573481234879}
\end{document}
-

Use the numprint package. The \npdecimalsign is used to specify that the decimal separator is to be a period, and \npthousandsep{ } define a space as a separator for the thousands and this also allows for line breaks within the number.

\documentclass{article}
\usepackage{numprint}
\npdecimalsign{\ensuremath{.}}%
\npthousandsep{ }%

\begin{document}
\begin{minipage}{0.35\linewidth}
\numprint{0.123719283791283718927489749875289345793485723495234572935239452348952384573481234879}
\end{minipage}
\end{document}

The minipage allows you to get the format in a more compact format.

Jake provides a method of indenting subsequent lines if you want that.

-

Well,

\documentclass{article}
\usepackage{numprint}
\npthousandsep{\hspace{0.3em}}

\begin{document}
\numprint{0.123719283791283718927489749875289345793485723495234572935239452348952384573481234879}
\end{document}
-

Now there is another way doing it, if for some reason you do not want to use additional packages, just plain simple LaTeX. The tabular environment is useful in this case:

\documentclass{minimal}

\begin{document}
\begin{tabular}{ccccccccccccc}
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
\end{tabular}
\end{document}

Now this does not work with the article documentclass, the spaces become very wide. But there is a solution to everything! :) Either insert @{} in the definition of the columns: \begin{tabular}{c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c@{}c} - then you can directly control the spacing by replacing $\;$ with wathever other spacing you need or you can remove the columns containing the spacing characters ($\;$ in this example) altogether, leaving it to LaTeX to set the spacing.

Another way to do it is with a tabbing environment:

\documentclass{article}

\begin{document}
\begin{tabbing}
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\\
\end{tabbing}
\end{document}

-
Did you try this with the article class? I'd rather use the tabbing environment. – egreg Sep 14 '11 at 9:34
Awww, shoot! Didn't think of that! Fixing it... – Count Zero Sep 14 '11 at 9:50