%%
%% This is file `numberpt.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% numberpt.dtx 
%% 
%% Copyright (C) 2019 Miguel V. S. Frasson (mvsfrasson@gmail.com)
%% 
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3c
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3c or later is part of all distributions of LaTeX
%% version 2008/05/04 or later.
%% 
%% This work has the LPPL maintenance status `maintained'.
%% 
%% The Current Maintainer of this work is the author.
%% 
%% This work consists of the files numberpt.dtx and numberpt.ins
%% and the derived file numberpt.sty.
%% 
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{numberpt}[2019/07/12 v1.0 Counters as numbers in Portuguese]
\RequirePackage{expl3,xparse}
\ExplSyntaxOn
\bool_new:N \NumberPT_catorze
\bool_new:N \NumberPT_dezesseis
\newcommand{\NumberPTcatorze}{\bool_set_true:N  \NumberPT_catorze}
\newcommand{\NumberPTquatorze}{\bool_set_false:N  \NumberPT_catorze}
\DeclareOption{ catorze  }{ \NumberPTcatorze }
\DeclareOption{ quatorze }{ \NumberPTquatorze }
\ExecuteOptions{ catorze }
\newcommand{\NumberPTdezesseis}{\bool_set_true:N  \NumberPT_dezesseis}
\newcommand{\NumberPTdezasseis}{\bool_set_false:N  \NumberPT_dezesseis}
\DeclareOption{ dezasseis  }{ \NumberPTdezasseis}
\DeclareOption{ dezesseis  }{ \NumberPTdezesseis}
\ExecuteOptions{ dezesseis }
\ProcessOptions
\bool_new:N \NumberPT_capital
\bool_new:N \NumberPT_capitalfirst
\bool_new:N \NumberPT_capitale
\bool_new:N \NumberPT_uppercase
\cs_new:Nn \NumberPT_case:nn {
  \bool_if:NTF \NumberPT_capital {
    \uppercase { #1 }
  } {
    #1
  }
  \bool_if:NTF \NumberPT_uppercase {
    \uppercase { #2 }
  } {
    #2 }
  \bool_if:NT \NumberPT_capitalfirst {
    \bool_set_false:N \NumberPT_capital
  }
}
\cs_new:Nn \NumberPT_u:n {
  \if_case:w #1
    \NumberPT_case:nn {z}{ero}\or:
    \NumberPT_case:nn {u}{m}\or:
    \NumberPT_case:nn {d}{ois}\or:
    \NumberPT_case:nn {t}{r\^es}\or:
    \NumberPT_case:nn {q}{uatro}\or:
    \NumberPT_case:nn {c}{inco}\or:
    \NumberPT_case:nn {s}{eis}\or:
    \NumberPT_case:nn {s}{ete}\or:
    \NumberPT_case:nn {o}{ito}\or:
    \NumberPT_case:nn {n}{ove}%
  \fi:}
\cs_new:Nn \NumberPT_e_u:n {
  \int_compare:nNnT #1 > 0 {
    \space \bool_if:nTF \NumberPT_capitale {E}{e} \space \NumberPT_u:n #1
  }
}
\cs_new:Nn \NumberPT_du:nn {
  \if_case:w #1 % #1=0
    \NumberPT_u:n #2
  \or: % #1=1
    \if_case:w #2
      \NumberPT_case:nn {d}{ez}\or:
      \NumberPT_case:nn {o}{nze}\or:
      \NumberPT_case:nn {d}{oze}\or:
      \NumberPT_case:nn {t}{reze}\or:
      \bool_if:NTF \NumberPT_catorze {
        \NumberPT_case:nn {c}{atorze}
      } {
        \NumberPT_case:nn {q}{uatorze}
      }\or:
      \NumberPT_case:nn {q}{uinze}\or:
      \bool_if:NTF \NumberPT_dezesseis {
        \NumberPT_case:nn {d}{ezesseis}
      } {
        \NumberPT_case:nn {d}{ezasseis}
      }\or:
      \bool_if:NTF \NumberPT_dezesseis {
        \NumberPT_case:nn {d}{ezessete}
      } {
        \NumberPT_case:nn {d}{ezassete}
      }\or:
      \NumberPT_case:nn {d}{ezoito}\or:
      \bool_if:NTF \NumberPT_dezesseis {
        \NumberPT_case:nn {d}{ezenove}
      } {
        \NumberPT_case:nn {d}{ezanove}
      }\or:
    \fi:
  \or: % #1>1
    \NumberPT_case:nn {v}{inte} \NumberPT_e_u:n #2
  \or:
    \NumberPT_case:nn {t}{rinta} \NumberPT_e_u:n #2
  \or:
    \NumberPT_case:nn {q}{uarenta} \NumberPT_e_u:n #2
  \or:
    \NumberPT_case:nn {c}{inquenta} \NumberPT_e_u:n #2
  \or:
    \NumberPT_case:nn {s}{essenta} \NumberPT_e_u:n #2
  \or:
    \NumberPT_case:nn {s}{etenta} \NumberPT_e_u:n #2
  \or:
    \NumberPT_case:nn {o}{itenta} \NumberPT_e_u:n #2
  \or:
    \NumberPT_case:nn {n}{oventa} \NumberPT_e_u:n #2
  \fi:
}

\cs_new:Nn \NumberPT_e_du:nn {
  \int_compare:nNnT { #1 + #2 } > 0 {
    \space \bool_if:nTF \NumberPT_capitale {E}{e} \space \NumberPT_du:nn #1 #2
  }
}
\cs_new:Nn \NumberPT_cdu:nnn {%
  \if_case:w #1 % #1=0
    \NumberPT_du:nn #2 #3
  \or: % #1=1
    \int_compare:nNnTF { #2 + #3 } = 0  % "cem" if 00 or "cento e " + finish
      { \NumberPT_case:nn {c}{em} }
      { \NumberPT_case:nn {c}{ento} \NumberPT_e_du:nn #2 #3 }
  \or: % #1>1
    \NumberPT_case:nn {d}{uzentos}  \NumberPT_e_du:nn #2 #3
  \or:
    \NumberPT_case:nn {t}{rezentos} \NumberPT_e_du:nn #2 #3
  \or:
    \NumberPT_case:nn {q}{uatrocentos} \NumberPT_e_du:nn #2 #3
  \or:
    \NumberPT_case:nn {q}{uinhentos} \NumberPT_e_du:nn #2 #3
  \or:
    \NumberPT_case:nn {s}{eiscentos} \NumberPT_e_du:nn #2 #3
  \or:
    \NumberPT_case:nn {s}{etecentos} \NumberPT_e_du:nn #2 #3
  \or:
    \NumberPT_case:nn {o}{itocentos} \NumberPT_e_du:nn #2 #3
  \or:
    \NumberPT_case:nn {n}{ovecentos} \NumberPT_e_du:nn #2 #3
  \fi:
}
\cs_new:Nn \NumberPT_e_cdu:nnn {
  \int_compare:nNnF {#1 + #2 + #3} = 0 {
    \space
    \bool_if:nT { \int_compare_p:nNn #1 = 0 || \int_compare_p:nNn {#2 + #3} = 0 }
    {
      \bool_if:nTF \NumberPT_capitale {E}{e}
      \space
    }
    \NumberPT_cdu:nnn #1 #2 #3
  }
}
\cs_new:Nn \NumberPT_abcdef:nnnnnn {
  \int_compare:nNnTF {#1 + #2 + #3} = 0 %abc=000
  {
    \NumberPT_cdu:nnn #4 #5 #6
  }
  % else
  {
    % avoid "um mil", using just "mil"
    \int_compare:nNnF {100 * #1 + 10 * #2 + #3} = 1 {
      \NumberPT_cdu:nnn #1 #2 #3
      \space
    }
    \NumberPT_case:nn {m}{il}
    \NumberPT_e_cdu:nnn #4 #5 #6
  }
}
\cs_generate_variant:Nn \NumberPT_abcdef:nnnnnn {xxxxxx}
\int_new:N \NumberPT_digit_u
\int_new:N \NumberPT_digit_d
\int_new:N \NumberPT_digit_c
\int_new:N \NumberPT_digit_um
\int_new:N \NumberPT_digit_dm
\int_new:N \NumberPT_digit_cm

\cs_new:Nn \NumberPT_decompose:n {
  \int_set:Nn \l_tmpa_int { #1 }
  \int_set:Nn \NumberPT_digit_u { \int_mod:nn { \l_tmpa_int } { 10 } }
  \int_set:Nn \l_tmpa_int { ( \l_tmpa_int - \NumberPT_digit_u ) / 10 }
  \int_set:Nn \NumberPT_digit_d { \int_mod:nn { \l_tmpa_int } { 10 } }
  \int_set:Nn \l_tmpa_int { ( \l_tmpa_int - \NumberPT_digit_d ) / 10 }
  \int_set:Nn \NumberPT_digit_c { \int_mod:nn { \l_tmpa_int } { 10 } }
  \int_set:Nn \l_tmpa_int { ( \l_tmpa_int - \NumberPT_digit_c ) / 10 }
  \int_set:Nn \NumberPT_digit_um { \int_mod:nn { \l_tmpa_int } { 10 } }
  \int_set:Nn \l_tmpa_int { ( \l_tmpa_int - \NumberPT_digit_um ) / 10 }
  \int_set:Nn \NumberPT_digit_dm { \int_mod:nn { \l_tmpa_int } { 10 } }
  \int_set:Nn \l_tmpa_int { ( \l_tmpa_int - \NumberPT_digit_dm ) / 10 }
  \int_set:Nn \NumberPT_digit_cm { \int_mod:nn { \l_tmpa_int } { 10 } }
}
\msg_new:nnn
{ numberpt } { counter-out-of-range } {Counter~`#1'~out~of~range~0~...~999999}

\cs_new:Nn \NumberPT_print_counter:n {
  \int_set:Nn \l_tmpa_int { \value{#1} }
  \bool_if:nTF {
    \int_compare_p:n { \l_tmpa_int >= 0 } &&
    \int_compare_p:n { \l_tmpa_int < 1000000 }
  }
  {
    \NumberPT_decompose:n { \l_tmpa_int }
    \NumberPT_abcdef:xxxxxx
    { \int_use:N \NumberPT_digit_cm }
    { \int_use:N \NumberPT_digit_dm }
    { \int_use:N \NumberPT_digit_um }
    { \int_use:N \NumberPT_digit_c  }
    { \int_use:N \NumberPT_digit_d  }
    { \int_use:N \NumberPT_digit_u  }
  }
  {
    \msg_error:nnn { numberpt } { counter-out-of-range } {#1}
    \arabic{#1}
  }
}
\NewDocumentCommand{\numberpt}{m}{
  \bool_set_false:N \NumberPT_capital
  \bool_set_false:N \NumberPT_capitale
  \bool_set_false:N \NumberPT_uppercase
  \NumberPT_print_counter:n { #1 }
}

\NewDocumentCommand{\NumberPt}{s m}{
  \bool_set_true:N \NumberPT_capital
  \bool_set:Nn \NumberPT_capitale {! #1}
  \bool_set_false:N \NumberPT_capitalfirst
  \bool_set_false:N \NumberPT_uppercase
  \NumberPT_print_counter:n { #2 }
}

\NewDocumentCommand{\Numberpt}{m}{
  \bool_set_true:N \NumberPT_capital
  \bool_set_false:N \NumberPT_capitale
  \bool_set_true:N \NumberPT_capitalfirst
  \bool_set_false:N \NumberPT_uppercase
  \NumberPT_print_counter:n { #1 }
}

\NewDocumentCommand{\NUMBERPT}{m}{
  \bool_set_true:N \NumberPT_capital
  \bool_set_true:N \NumberPT_capitale
  \bool_set_true:N \NumberPT_uppercase
  \NumberPT_print_counter:n { #1 }
}
\ExplSyntaxOff
\endinput
%%
%% End of file `numberpt.sty'.
