%% This is file `sepnum.sty' -- separated numbers
%%
%% (C) 1995, 1996, 2001 Bernd Raichle
%%                      Raichle@Informatik.Uni-Stuttgart.de
%%
%% This package provides a means of formatting numbers
%% with (a) a decimal separator different of the default (`.')
%% and (b) a separator (default `,') every three digits.
%%
%% First version of this package was posted to the newsgroup
%% `de.comp.tex' November 6th, 1995 (as `printnum.sty').
%%
%%
%% -- LICENSING CONDITION --
%%
%% This program may be redistributed and/or modified under the terms
%% of the LaTeX Project Public License, either version 1.0 of this
%% license, or (at your option) any later version.
%% The latest version of this license is in
%%   CTAN:macros/latex/base/lppl.txt.
%%
%%
%% -- USAGE --
%%
%% The macro
%%
%%    \sepnum{<c>}{<sbc>}{<sac>}{<number>}
%%
%% produces the <number> with the decimal point replaced by <c>
%% and the separator <sbc> every three digits in the integer part,
%% the separator <sac> every three digits in the fractional part
%% of <number>.  \sepnum is _fully expandable_ and _not_ fragile,
%% unless one of <c>, <sbc>, or <sac> is.
%%
%% Example:
%%   - a non realistic example to show the capabilities of \sepnum
%%     \sepnum{,}{*}{-}{1234.5678} => 1*234,567-8
%%   - to print a number inserting small spaces each three digits
%%     \sepnum{.}{\,}{\,}{1234.5678} => 1\,234.567\,8
%%
%%
%% Additionally this package provides
%%
%%   \sepnumform{<c>}{<sbc>}{<sac>}{<LaTeX-counter>}
%%
%% to use \sepnum with a LaTeX counter.
%%
%% Example (with a decimal comma in the print form \the<counter>):
%%   \newcounter{money}
%%   \renewcommand{\themoney}{\sepnumform{,}{.}{}{money}}
%%
%%   \setcounter{money}{-1000}
%%   You have \themoney{} left.
%%
%%
%% (For compatibility with `printnum.sty', the macros
%% \printnum{<number>} and \printnumKomma/\printnumTrenner
%% still exist.)
%%
%%
%% -- SEE ALSO --
%%
%% * Package ``comma.sty'' by David Carlisle which provides
%%   similar features for integers only, i.e. it misses support
%%   for the decimal point/comma in fixed point numbers.
%% * Package ``numprint.sty'' by Harald Harders supporting
%%   formatting of numbers given as 1234e5 in addition.
%%   
%%
\begingroup \expandafter\expandafter\expandafter\endgroup
\expandafter\ifx \csname ProvidesPackage\endcsname\relax \else
  \ProvidesPackage{sepnum}%
      [1996/09/05 v2.0 Insert a separator every three digits (br)]
\fi


%
% auxiliary macros for loops:
%
\newcommand{\sepnumReturnFi}{}
\newcommand{\sepnumReturnElseFi}{}
\newcommand{\sepnumReturnOrFi}{}

\def\sepnumReturnFi#1\fi{\fi #1}
\def\sepnumReturnElseFi#1\else#2\fi{\fi #1}
\def\sepnumReturnOrFi#1\or#2\fi{\fi #1}

%
% \sepnum@DoNothing #1
%    don't change #1 (removes braces around argument token list!)
%
\newcommand{\sepnum@DoNothing}[1]{#1}

%
% \sepnum@ReverseNumber #1 #2
%    reverses all tokens in #2 and calls #1{#2} afterwards
%
\newcommand{\sepnum@ReverseNumber}[2]{%
  \sepnum@DoReverseNumber{#1}{}#2\relax}
\newcommand{\sepnum@DoReverseNumber}[3]{%
  \ifx\relax#3\sepnumReturnElseFi{#1{#2}}%
  \else\sepnumReturnFi{\sepnum@DoReverseNumber{#1}{#3#2}}%
  \fi}

%
% \sepnum@ThreeDigits #1 #2 #3
%    reverses #3, inserts #1 every three tokens/digits
%    (you have to reverse #3 again using \sepnum@ReverseNumber!),
%    calls #2{#3} afterwards
%
\newcommand{\sepnum@ThreeDigits}[3]{%
  \sepnum@DoThreeDigits{#1}{#2}{}0#3\relax}
\newcommand{\sepnum@DoThreeDigits}[5]{%
  \ifx\relax#5\sepnumReturnElseFi{#2{#3}}%
  \else\sepnumReturnFi{%
    \ifcase#4\noexpand\sepnum@DoNothing{}% 0
      \sepnumReturnOrFi{\sepnum@DoThreeDigits{#1}{#2}{#5#3}1}%
    \or   % 1
      \sepnumReturnOrFi{\sepnum@DoThreeDigits{#1}{#2}{#5#3}2}%
    \or   % 2
      \sepnumReturnElseFi{\sepnum@DoThreeDigits{#1}{#2}{#5#3}3}%
    \else % 3  ==> insert #1
      \sepnumReturnFi{\sepnum@DoThreeDigits{#1}{#2}{#5#1#3}1}%
    \fi}%
  \fi}

%
% \sepnum@BeforeComma #1 #2
% \sepnum@AfterComma #1 #2
%    call \sepnum@ReverseNumber and \sepnum@DoThreeDigits
%    to correctly group the number #2 every three digits
%    either with or without reversing first or afterwards.
%
\newcommand{\sepnum@BeforeComma}[2]{%
  \sepnum@ReverseNumber{\sepnum@ThreeDigits{#1}\sepnum@DoNothing}{#2}}
\newcommand{\sepnum@AfterComma}[2]{%
  \sepnum@ThreeDigits{#1}{\sepnum@ReverseNumber\sepnum@DoNothing}{#2}}

%
%%%%%%%%%% USER MACRO %%%%%%%%%%
%
% \sepnum #1 #2 #3 #4
%
%    #1 decimal comma
%    #2 separator for three digits groups before comma
%    #3 separator for three digits groups after comma
%    #4 number
%
\newcommand{\sepnum}[4]{%
  \sepnum@Sign{#1}{#2}{#3}#4.\relax}
%
%    look first for sign `+' or `-', divide given number in
%    digits before and after `.', insert #1 as decimal comma
%    (in \sepnum@SepComma)
%
\newcommand{\sepnum@Sign}[4]{%
  \ifx\relax#4%
    0%
  \else \sepnumReturnFi{%
    \ifx -#4%
      \sepnumReturnElseFi{-\sepnum@SepComma{#1}{#2}{#3}}%
    \else \sepnumReturnFi{%
      \ifx +#4%
        \sepnumReturnElseFi{+\sepnum@SepComma{#1}{#2}{#3}}%
      \else
        \sepnumReturnFi{\sepnum@SepComma{#1}{#2}{#3}#4}%
      \fi}%
    \fi}%
  \fi}

\newcommand{\sepnum@SepComma}{}
\def\sepnum@SepComma#1#2#3#4.#5\relax{%
  \ifx$#4$0% no digits before `.' => add 0 for "0.xxx"
  \else \sepnumReturnFi{\sepnum@BeforeComma{#2}{#4}}\fi
  \ifx$#5$% no digits after `.'
  \else \sepnumReturnFi{#1\sepnum@DoSepComma{#3}#5\relax}\fi}

% remove `.' inserted in \sepnum
\def\sepnum@DoSepComma#1#2.#3\relax{%
  \sepnum@AfterComma{#1}{#2}}


%
%%%%%%%%%% USER MACRO %%%%%%%%%%
%
% \sepnumform #1 #2 #3 #4
%
%    like \sepnum but #4 is the name of a LaTeX counter
%
\newcommand{\sepnumform}[4]{%
  \expandafter\sepnum@doform
  \expandafter{\number\value{#4}}{#1}{#2}{#3}}
\newcommand{\sepnum@doform}[4]{%
  \sepnum{#2}{#3}{#4}{#1}}


%
%%%%%%%%%% USER MACRO %%%%%%%%%%
%
% For compatibility with `printnum.sty':
%
% \printnum #1
%
\newcommand{\printnum}[1]{%
  \sepnum{\printnumKomma}{\printnumTrenner}{\printnumTrenner}{#1}}
%
% Macros to specify comma and separator (default to get
% european style numbers, e.g. "123.456,78" for "123456.78"):
%
%   \printnumKomma    => ,
%   \printnumTrenner  => .
%
\newcommand{\printnumKomma}{,}
\newcommand{\printnumTrenner}{.}

\endinput
%%
%% End of file `sepnum.sty'.
