# Grouping digits according to the Indian numbering system in siunitx

\num{100000} prints 100 000

I want to have 1 00 000.

Question: How to set siunitx package to do the above?

Some more

\num{1000} => 1 000

\num{10000} => 10 000

\num{100000} => 1 00 000

\num{1000000} => 10 00 000

\documentclass[12pt]{article}
\usepackage{siunitx}
\sisetup{group-separator = {,}}

\begin{document}

\num{1000}

\num{10000}

\num{100000}

\num{1000000}

\num{10000000}

\num{100000000}

\num{1000000000}

\SI{1000000000}{m}

\end{document}


The exact rules are described on the Wikipedia entry:

The Indian Numbering System uses separators differently from the Arabic system; besides the three least significant digits of the integer part, a comma divides every two rather than every three digits.

• What are the precise rules? – egreg Nov 14 '13 at 13:45
• It seems to be: the rightmost group is of three digits, the others are groups of two digits (or one for the first). It doesn't seem difficult to implement, but I suspect it needs a specific set of macros in siunitx. – egreg Nov 14 '13 at 13:51

In the meantime before an actual siunitx solution arrives here, you can use this expandable macro. But it will work only on (long) integers. For numbers in scientific notation you need something which extracts the mantissa, and then you can feed \indianum with this mantissa.

\documentclass[12pt]{article}
%\usepackage{siunitx}
%\sisetup{group-separator = {,}}

\usepackage{xint}

\makeatletter
\newcommand\indianum[1]
{\expandafter\indianum@\romannumeral-0#1!}
\def\indianum@ #1{\ifx #1-\expandafter\@firstoftwo
\else\expandafter\@secondoftwo\fi
{\expandafter-\romannumeral-0\indianum@a}
{\indianum@a#1}}

\def\indianum@a #1!{\expandafter\indianum@b\expandafter
{\romannumeral-0\xintLength{#1}}#1!}

\def\indianum@b #1{\xintifGt {#1}{3}
{\xintifOdd {#1}{\indianum@codd }
{\indianum@ceven }}
{\indianum@short}}

\def\indianum@short #1!{#1}

\def\indianum@codd #1#2#3#4{\indianum@loop {#1#2,}{#3#4}}

\def\indianum@ceven #1#2#3{\indianum@loop {#1,}{#2#3}}

\def\indianum@loop #1#2#3#4{\ifx #4!\expandafter\@firstoftwo
\else\expandafter\@secondoftwo\fi
{#1#2#3}{\indianum@loop {#1#2,}{#3#4}}}
\makeatother

\begin{document}
\indianum{1}

\indianum{10}

\indianum{100}

\indianum{1000}

\indianum{10000}

\indianum{100000}

\indianum{1000000}

\indianum{10000000}

\indianum{100000000}

\indianum{1000000000}

\indianum{-1}

\indianum{-10}

\indianum{-100}

\indianum{-1000}

\indianum{-10000}

\indianum{-100000}

\indianum{-1000000}

\indianum{-10000000}

\indianum{-100000000}

\indianum{-1000000000}

\end{document}


2017 edit (converted from a comment)

It is possible to load only xintkernel which is very small subset of xint, containing its \xintLength macro, and load etoolbox for its integer comparison conditionals in replacement of the \xintifGt and \xintifOdd conditionals of xint, the tested numbers being short enough certainly.

As etoolbox is often in use, this may be useful if the macros of xint are not needed elsewhere (and it is even possible to simply copy over from etoolbox package its definitions of \ifnumgreater and \ifnumodd which are slim wrappers of TeX primitives; then only package xintkernel is needed.)

Here is the modified example, produces same output:

\documentclass[12pt]{article}
%\usepackage{siunitx}
%\sisetup{group-separator = {,}}

\usepackage{etoolbox}
\usepackage{xintkernel}

\makeatletter
\newcommand\indianum[1]{\expandafter\indianum@\romannumeral-0#1!}

\def\indianum@ #1{\ifx #1-\expandafter\@firstoftwo
\else\expandafter\@secondoftwo\fi
{\expandafter-\romannumeral-0\indianum@a}
{\indianum@a#1}}

\def\indianum@a #1!{\expandafter\indianum@b\expandafter
{\romannumeral-0\xintLength{#1}}#1!}

\def\indianum@b #1{\ifnumgreater {#1}{3}
{\ifnumodd {#1}{\indianum@codd }
{\indianum@ceven }}
{\indianum@short}}

\def\indianum@short #1!{#1}

\def\indianum@codd #1#2#3#4{\indianum@loop {#1#2,}{#3#4}}

\def\indianum@ceven #1#2#3{\indianum@loop {#1,}{#2#3}}

\def\indianum@loop #1#2#3#4{\ifx #4!\expandafter\@firstoftwo
\else\expandafter\@secondoftwo\fi
{#1#2#3}{\indianum@loop {#1#2,}{#3#4}}}
\makeatother

\begin{document}
\indianum{1}

\indianum{10}

\indianum{100}

\indianum{1000}

\indianum{10000}

\indianum{100000}

\indianum{1000000}

\indianum{10000000}

\indianum{100000000}

\indianum{1000000000}

\indianum{-1}

\indianum{-10}

\indianum{-100}

\indianum{-1000}

\indianum{-10000}

\indianum{-100000}

\indianum{-1000000}

\indianum{-10000000}

\indianum{-100000000}

\indianum{-1000000000}

\end{document}