Formatting Decimals

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.

enter image description here

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


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

Tags:

Formatting