% This file is part of babel. For further details see:
% https://www.ctan.org/pkg/babel
\ifx\BabelBeforeIni\undefined
  \PackageError{babel}%
    {This file is a component of babel and cannot\MessageBreak
     be loaded directly. I'll stop immediately}%
    {Just use babel as documented.}%
  \stop
\fi
\BabelBeforeIni{he}{%
}

% Contributed by Udi Fogiel (with some changes)
\def\BabelHebrewNumeralMarkerSingle{׳}% Number is a single letter
\def\BabelHebrewNumeralMarkerFinal{״}%  Before final letter

\@namedef{bbl@cntr@letters.plain@\CurrentOption}#1%
  {\expandafter\@hebrew@@numeral\expandafter{\the\numexpr#1}0}

\@namedef{bbl@cntr@letters.gershayim@\CurrentOption}#1%
  {\expandafter\@hebrew@@numeral\expandafter{\the\numexpr#1}1}

\@namedef{bbl@cntr@letters.final@\CurrentOption}#1%
  {\expandafter\@hebrew@@numeral\expandafter{\the\numexpr#1}2}

\def\hebrew@num@trunc#1#2{%
  \ifnum\numexpr #1-#2*\numexpr #1/#2\relax<0
    \the\numexpr #1/#2-1\relax
  \else
    \the\numexpr #1/#2\relax
  \fi}
  
\def\@hebrew@@numeral#1#2{%
  \ifnum#1<\z@\bbl@error{Invalid Hebrew numeral (<0)}{Not supported.}%
  \else
    \ifnum#1<1000
		    \hebrew@num@nomil{#1}{#2}%
    \else
      \ifnum#1>999999 % For forward compatibility
        \bbl@error{Hebrew numeral too large (>999999)}%
          {Currently this is the limit.}%
        #1%
      \else
        \expandafter\@hebrew@@numeral\expandafter
          {\the\numexpr\hebrew@num@trunc{#1}{1000}}0%
        \BabelHebrewNumeralMarkerSingle
        \expandafter\hebrew@num@nomil\expandafter
          {\the\numexpr #1-1000*\hebrew@num@trunc{#1}{1000}}{#2}%
      \fi
    \fi
  \fi}
  
\def\hebrew@alph@zero{}
\def\hebrew@num@nomil#1#2{%
  \ifcase\hebrew@num@trunc{#1}{100} %
    \or ק\ifnum#2>0 \ifnum#1=100 \BabelHebrewNumeralMarkerSingle\fi\fi
    \or ר\ifnum#2>0 \ifnum#1=200 \BabelHebrewNumeralMarkerSingle\fi\fi
    \or ש\ifnum#2>0 \ifnum#1=300 \BabelHebrewNumeralMarkerSingle\fi\fi
    \or ת\ifnum#2>0 \ifnum#1=400 \BabelHebrewNumeralMarkerSingle\fi\fi
    \or ת\ifnum#2>0 \ifnum#1=500 \BabelHebrewNumeralMarkerFinal\fi\fi ק%
    \or ת\ifnum#2>0 \ifnum#1=600 \BabelHebrewNumeralMarkerFinal\fi\fi ר%
    \or ת\ifnum#2>0 \ifnum#1=700 \BabelHebrewNumeralMarkerFinal\fi\fi ש%
    \or ת\ifnum#2>0 \ifnum#1=800 \BabelHebrewNumeralMarkerFinal\fi\fi ת%
    \or תת\ifnum#2>0 \ifnum#1=900 \BabelHebrewNumeralMarkerFinal\fi\fi ק%
  \fi
  \ifnum\the\numexpr #1-100*\hebrew@num@trunc{#1}{100} = 16
    \ifnum#2>0 ט\BabelHebrewNumeralMarkerFinal ז\else טז\fi  % tet-zayin
  \else\ifnum\the\numexpr #1 - 100*\hebrew@num@trunc{#1}{100} = 15
    \ifnum#2>0 ט\BabelHebrewNumeralMarkerFinal ו\else טו\fi  % tet-vav
  \else  % print nothing if no tens:
  \ifcase\expandafter\hebrew@num@trunc\expandafter
      {\the\numexpr#1-100*\hebrew@num@trunc{#1}{100}}{10}% 
    \or\ifnum\numexpr #1-10*\hebrew@num@trunc{#1}{10}>0 י\else
        \ifnum#2>0 \ifnum#1=10 י\BabelHebrewNumeralMarkerSingle
        \else\BabelHebrewNumeralMarkerFinal י\fi\else י\fi
      \fi
    \or\ifnum\numexpr #1-10*\hebrew@num@trunc{#1}{10}>0 כ\else
         \ifnum#2>0 \ifnum#1>20 \BabelHebrewNumeralMarkerFinal\fi\fi
         \if #22ך\else כ\fi\ifnum#2>0 \ifnum#1=20 \BabelHebrewNumeralMarkerSingle\fi\fi
       \fi
    \or\ifnum\numexpr #1-10*\hebrew@num@trunc{#1}{10}>0 ל\else
         \ifnum#2>0 \ifnum#1=30 ל\BabelHebrewNumeralMarkerSingle
         \else\BabelHebrewNumeralMarkerFinal ל\fi\else ל\fi
       \fi
    \or\ifnum\numexpr #1-10*\hebrew@num@trunc{#1}{10}>0 מ\else
         \ifnum#2>0 \ifnum#1>40 \BabelHebrewNumeralMarkerFinal\fi\fi
         \if #22ם\else מ\fi\ifnum#2>0 \ifnum#1=40 \BabelHebrewNumeralMarkerSingle\fi\fi
       \fi
    \or\ifnum\numexpr #1-10*\hebrew@num@trunc{#1}{10}>0 נ\else
         \ifnum#2>0 \ifnum#1>50 \BabelHebrewNumeralMarkerFinal\fi\fi
         \if #22ן\else נ\fi\ifnum#2>0 \ifnum#1=50 \BabelHebrewNumeralMarkerSingle\fi\fi
       \fi
    \or\ifnum\numexpr #1-10*\hebrew@num@trunc{#1}{10}>0 ס\else
         \ifnum#2>0 \ifnum#1=60 ס\BabelHebrewNumeralMarkerSingle
         \else\BabelHebrewNumeralMarkerFinal ס\fi\else ס\fi
       \fi
    \or\ifnum\numexpr #1-10*\hebrew@num@trunc{#1}{10}>0 ע\else
         \ifnum#2>0 \ifnum#1=70 ע\BabelHebrewNumeralMarkerSingle
         \else\BabelHebrewNumeralMarkerFinal ע\fi\else ע\fi
       \fi
    \or\ifnum\numexpr #1-10*\hebrew@num@trunc{#1}{10}>0 פ\else
         \ifnum#2>0 \ifnum#1>80 \BabelHebrewNumeralMarkerFinal\fi\fi
         \if #22ף\else פ\fi\ifnum#2>0 \ifnum#1=80 \BabelHebrewNumeralMarkerSingle\fi\fi
       \fi
    \or\ifnum\numexpr #1-10*\hebrew@num@trunc{#1}{10}>0 צ\else
        \ifnum#2>0 \ifnum#1>90 \BabelHebrewNumeralMarkerFinal\fi\fi
        \if #22ץ\else צ\fi\ifnum#2>0 \ifnum#1=90 \BabelHebrewNumeralMarkerSingle \fi\fi
      \fi
  \fi
  \ifcase\numexpr #1-10*\hebrew@num@trunc{#1}{10} %
      \hebrew@alph@zero %  empty but can be defined if desired
      \or\ifnum#2>0 \ifnum#1=1 א\BabelHebrewNumeralMarkerSingle
        \else\BabelHebrewNumeralMarkerFinal א\fi\else א\fi
      \or\ifnum#2>0 \ifnum#1=2 ב\BabelHebrewNumeralMarkerSingle
        \else\BabelHebrewNumeralMarkerFinal ב\fi\else ב\fi
      \or\ifnum#2>0 \ifnum#1=3 ג\BabelHebrewNumeralMarkerSingle
        \else\BabelHebrewNumeralMarkerFinal ג\fi\else ג\fi
      \or\ifnum#2>0 \ifnum#1=4 ד\BabelHebrewNumeralMarkerSingle
        \else\BabelHebrewNumeralMarkerFinal ד\fi\else ד\fi
      \or\ifnum#2>0 \ifnum#1=5 ה\BabelHebrewNumeralMarkerSingle
        \else\BabelHebrewNumeralMarkerFinal ה\fi\else ה\fi
      \or\ifnum#2>0 \ifnum#1=6 ו\BabelHebrewNumeralMarkerSingle
        \else\BabelHebrewNumeralMarkerFinal ו\fi\else ו\fi
      \or\ifnum#2>0 \ifnum#1=7 ז\BabelHebrewNumeralMarkerSingle
        \else\BabelHebrewNumeralMarkerFinal ז\fi\else ז\fi
      \or\ifnum#2>0 \ifnum#1=8 ח\BabelHebrewNumeralMarkerSingle
        \else\BabelHebrewNumeralMarkerFinal ח\fi\else ח\fi
      \or\ifnum#2>0 \ifnum#1=9 ט\BabelHebrewNumeralMarkerSingle
        \else\BabelHebrewNumeralMarkerFinal ט\fi\else ט\fi
  \fi\fi\fi}

\endinput