# How do I split large numbers into groups of five digits? [duplicate]

I need to split a number of 27 digits into groups of 5 digits. I already had a look at siunitx, telprint and numprint. But these just allow me to generate groups of two or three digits. Is there any known way to do split the number into groups of 5?

-

## marked as duplicate by egreg macros StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); May 14 '14 at 8:51

27 is not multiple of 5. Which group has to have 2 digits, the leftmost or the rightmost? – JLDiaz May 14 '14 at 7:24
i need the two digits at the leftmost part of that number – Jakob May 14 '14 at 8:15

Grouping from the right side:

\documentclass[a4paper]{article}
\usepackage{xstring}

\makeatletter
% Low level macro
% #1 String to separate in groups of five
% #2 Character to insert as separator
\def\@separatefivedigits#1#2{%
\StrLen{#1}[\length]%
\ifnum\length<5\relax#1\else%
\bgroup
\StrRight{#1}{5}[\lowerfive]%
\StrGobbleRight{#1}{5}[\remaining]%
\@separatefivedigits{\remaining}{#2}#2\lowerfive%
\egroup
\fi}

% User macro
% #1 String to separate in groups of five
% #2 Character to insert as separator
\def\formatnumber#1#2{\StrDel{#1}{ }[\aux]%
\@separatefivedigits{\aux}{#2}%
}

% Convenience macro for the case in which the separator is the space
\def\formatnumberwithspaces#1{\formatnumber{#1}{ }}
\makeatother

\begin{document}
% Examples
\formatnumberwithspaces{1001234512345123451234512345123451234512345}\par
\formatnumber{1001234512345123451234512345123451234512345}{$\cdot$}
\end{document}


Produces

-

Grouping from the left side:

\documentclass{article}
\makeatletter
\newcount\c@Digits
\def\GroupDigits#1{%
\global\c@Digits=0
\expandafter\GroupDigits@i#1\@nil}
\def\GroupDigits@i#1#2\@nil{%
#1%
\ifnum\c@Digits=5 \,\c@Digits=\z@\fi
\ifx\relax#2\relax\else\GroupDigits@i#2\@nil\fi
}
\makeatother
\begin{document}
\GroupDigits{123456789}

\GroupDigits{123456789012345678901234567}
\end{document}


-

Just taking comma.sty from ctan and changing #1#2#3 to #1#2#3#4#5

\documentclass{article}

\makeatletter

%%% comma.sty
%%%
%%% Copyright 1996 1997 David Carlisle
%%%
%%% This file may be distributed under the terms of the LPPL.
%%%
%%% David Carlisle david@dcarlisle.demon.co.uk
%%%
%\ProvidesPackage{comma}
%      [1997/12/15 v1.2 Insert commas every three digits (DPC)]

%% This package provides a means of producing numbers with a separator
%% (by default a comma) every three digits.
%%
%% Given a LaTeX counter (eg section)
%%
%% \renewcommand\thesection{\commaform{section}}
%%
%% If section is 12345, \thesection will now print as 12,345
%%
%% If you want something other than a comma, for instance a thin
%% space, or a full word space, redefine \commaformtoken, for instance
%% \renewcommand\commaformtoken{\,}
%% \renewcommand\commaformtoken{ }
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% v1.0 1996/09/05
% v1.1 1997/07/10
% v1.2 1997/12/15

% #1 is the name of a LaTeX counter.
\def\commaform#1{%
\expandafter\@commaform\csname c@#1\endcsname}

%     The token to place every three digits.
\def\commaformtoken{,}

% Internal version.
% #1 is the number. It may be a TeX count register, eg \count@
%    or an explicit number such as 1234'.
% v1.1 use \relax not empty so explicit digits need not end with a space
% token.
% (This \relax will be eaten by the parser, second two must be \@empty)
% v1.2 add \@comma@ux for very long digit strings (requested on c.t.t)
\def\@commaform#1{%
\expandafter\@commaaux
\expandafter{\expandafter}%
\number\@comma@ux#1%
\relax\@empty\@empty\@empty\@empty}

% If \@commaform is given a very long digit string then it may be
% too large for \number, so make sure \number only applies to the first
% token. This may leave a spurious space token, but it will be eaten
% by the \@commaaux parsing, which uses non delimited arguments, and
% so skips space tokens.
\def\@comma@ux#1{#1 }

% Wander down to the end of the number and then see where
%    \relax turns up.
% #1 List of digits already seen (initially {} )
% #2#3#4 next three digits (or \@empty) in list.
\def\@commaaux#1#2#3#4#5#6{%
\ifx\relax#2%
\else\ifx\relax#3%
\else\ifx\relax#4%
\else\ifx\relax#5%
\else\ifx\relax#6%
\else\@commaauxafterfi{#1#2#3#4#5#6}%
\fi
\fi
\fi
\fi
\fi}

% Get out of the nested \if before recursing down the list of digits.
% #1 list of digits seen so far.
\def\@commaauxafterfi#1\fi\fi\fi\fi\fi{%
\fi\fi\fi\fi\fi\@commaaux{#1}}

% Go down adding a comma' every three tokens. The list will have
%    been padded with 0 1 or 2  \@empty at the start so there is
%    definitely a multiple of three tokens before the \relax.
% #1#2#3 are next three digits
% #4 is next digit, or \relax to stop
#1#2#3#4#5%
\if#6\relax
\else
\commaformtoken
\fi}

\let\xcommaform\@commaform

\makeatletter

\begin{document}

\xcommaform{1}

\xcommaform{123}

\xcommaform{12345}

\xcommaform{1234567}

\xcommaform{11111222223333344444}

\xcommaform{11122222333334444455555}

\end{document}

-