%D \module
%D   [       file=t-counting,
%D        version=2005.08.06,
%D          title=Cardinal and Ordinal Numbers with Words,
%D         author=Mojca Miklavec,
%D           date=\currentdate]
%D
%D Description: TODO
%D
%D
%D Currently it can only handle 32-bit numbers,
%D so everything between -2.147.483.848 and 2.147.483.847.
%D
%D However, it should not be so difficult to add support for longer numbers.
%D Someone should write a macro \type{\[en]longcardinalnumber},
%D which would split a very long number and call \type{\[en]cardinalnumber}.
%D Or one should modify \type{\[en]cardinalnumber}, so that it wouldn't use
%D \type{\ifnum}, \type{\numexpr} and \type{\newcount},
%D but count the digits instead.
%D
%D \useURL
%D     [cardinal numbers]
%D     [http://www.ego4u.com/en/cram-up/vocabulary/numbers/cardinal][]
%D     [http://www.ego4u.com/en/cram-up/vocabulary/numbers/cardinal]
%D Source: \from[cardinal numbers]

\unprotect

%D
%D English Cardinal Numbers
%D

\def\encardinalnumber#1%
	{\expandafter\doencardinalnumber\number#1\relax}

% some settings

% TODO:
% - better names for the two options
% - it has to be made local and and accessible by
%   something like \setupencardilnumbers[withand=true/false,startwith=a/one]
% - using "a" instead of "one" is not supported yet
%   (the macro has to be changed: we have to know if we're at the begining or not)
\newif\ifencardinalnumberwithandafterhundred % afterhundred may be dropped
% in American English, this may be false, in British English, it has to be true
\encardinalnumberwithandafterhundredtrue
%
% at the beginning of the word, it is possible to say
% "a hundred" instead of "one hundred" (one thousand, one million, ...)
% however, "one" is the default
\newif\ifencardinalnumbersstartwithone
\encardinalnumbersstartwithonetrue

\def\doencardinalnumber#1#2\relax
	% some special care hase to be taken for negative numbers
	% (perhaps there's a better solution than this one)
	{\ifnum#1#2<0
		minus \encardinalnumber{#2}%
	%----------------------%
	% numbers from 0 to 19 %
	%----------------------%
	% everything written together
	%
	\else\ifnum#1#2<20
		\ifcase#1#2 zero\or one\or two\or three\or four\or five\or six\or seven\or
			eight\or nine\or ten\or eleven\or twelve\or thirteen\or fourteen\or
			fifteen\or sixteen\or seventeen\or eighteen\or nineteen\fi
	%------------------%
	% numbers up to 99 %
	%------------------%
	% tens [- 1-9]
	%
	\else\ifnum#1#2<100
		\ifcase#1 \or\or twenty\or thirty\or forty\or fifty\or sixty\or seventy\or
			eighty\or ninety\fi%
		% TODO: does a hyphen have to be written differently
		%       (for example to prevent breaking)?
		\ifnum#2>0 -\encardinalnumber{#2}\fi%
	%-------------------%
	% numbers up to 999 %
	%-------------------%
	% 1-9 hundred [[and] 1-99]
	%
	\else\ifnum#1#2<1000
		% TODO: \ifencardinalnumbersstartwithone & \ifnum#1=1 & "this is start"
		\encardinalnumber{#1} hundred% TODO: how to handle spacing here?
		\ifnum#2>0
			\ % space between hundred and the number following
			% leave "and" out in American English (if requested)
			\ifencardinalnumberwithandafterhundred and \fi
			\encardinalnumber{#2}%
		\fi
	%-----------------------%
	% numbers up to 999.999 %
	%-----------------------%
	% 1-999 thousand [[and] 1-999]
	%
	\else\ifnum#1#2<1000000
		% cnt@A := number / 1000
		\newcount\cnt@A\cnt@A=#1#2
		\divide\cnt@A by 1000
		\encardinalnumber{\the\cnt@A} thousand%
		% cnt@A := number % 1000
		\multiply\cnt@A by-1000
		\advance\cnt@A by #1#2
		\ifnum\cnt@A>0
			\ % space between thousand and the number following
			% leave "and" out in American English (if requested)
			\ifencardinalnumberwithandafterhundred and \fi
			\encardinalnumber{\the\cnt@A}%
		\fi
	%---------------------------%
	% numbers up to 999.999.999 %
	%---------------------------%
	% 1-999 million[, 1-999.999]
	%
	\else\ifnum#1#2<1000000000
		% cnt@A := number / 1.000.000
		\newcount\cnt@A\cnt@A=#1#2
		\divide\cnt@A by 1000000
		\encardinalnumber{\the\cnt@A} million%
		% cnt@A := number % 1.000.000
		\multiply\cnt@A by-1000000
		\advance\cnt@A by #1#2
		\ifnum\cnt@A>0
			,\ % comma and space between billion and the number following
			\encardinalnumber{\the\cnt@A}%
		\fi
	%-----------------------------%
	% numbers up to 2.147.483.847 %
	%-----------------------------%
	% 1-999 billion[, 1-999.999] (the British billion USED TO be 10^12)
	%
	\else
		% cnt@A := number / 1.000.000.000
		\newcount\cnt@A\cnt@A=#1#2
		\divide\cnt@A by 1000000000
		\encardinalnumber{\the\cnt@A} billion%
		% cnt@A := number % 1.000.000.000
		\multiply\cnt@A by-1000000000
		\advance\cnt@A by #1#2
		\ifnum\cnt@A>0
			,\ % comma and space between trillion and the number following
			\encardinalnumber{\the\cnt@A}%
		\fi
	\fi\fi\fi\fi\fi\fi}
	%
	% Theoretically, this could handle longer numbers, but for now
	% we're limited to the 32 bits of TeX.
	% If someone volunteers to help me overcome this obstacle by
	% counting the number of digits instead of using the TeX built-in
	% arithmetics (it should be very easy), the macro can be extended.
	%
	% Next comes trillion, quadrillion, ... but once you manage to typeset that many
	% chapters (in case you would like to number the chapters with this macro),
	% TeX will run out of memory anyway ;)
	%

\defineconversion [encardinalnumber]    [\encardinalnumber]

%D
%D German Cardinal numbers
%D

\def\decardinalnumber#1%
	{\expandafter\dodecardinalnumber\number#1\relax}

% TODO: these two functions have to be made local to \dodecardinalnumber
% I didn't exactly understand the mechanism above, so I simply copied the macro.
% But \dodecardinalnumberones only needs one parameter, not two
% As a matter of fact, this is not a 100% satisfactory solution as some numbers
% (esp. "1") may be declined anyway
\def\decardinalnumberones#1%
	{\expandafter\dodecardinalnumberones\number#1\relax}
\def\dodecardinalnumberones#1#2\relax
	{\ifcase#1#2\or ein\or zwei\or drei\or vier\or f\uumlaut nf\or
		sechs\or sieben\or acht\or neun\fi}

\def\dodecardinalnumber#1#2\relax
	{\ifnum#1#2<0
		minus \decardinalnumber{#2}%
	% numbers from 0 to 19
	\else\ifnum#1#2<20
		\ifcase#1#2 null\or eins\or zwei\or drei\or vier\or f\uumlaut nf\or
			sechs\or sieben\or acht\or neun\or zehn\or elf\or zw\oumlaut lf\or
			dreizehn\or vierzehn\or f\uumlaut nfzehn\or sechzehn\or siebzehn\or
			achtzehn\or neunzehn\fi
	% numbers up to 99
	\else\ifnum#1#2<100
		% for numbers like 20, 30, 40, ... no preposition is needed
		% for numbers like 21, 31, 41, ... "einund" is added (einundzwanzig, ...)
		% the rest is composed from the ones and the tens (zweiundzwanzig, ...)
		\ifnum#2>0 \decardinalnumberones{#2}und%
		\fi%
		\ifcase#1 \or\or zwanzig\or drei\ssharp ig\or vierzig\or f\uumlaut nfzig\or
			sechzig\or siebzig\or achtzig\or neunzig\fi
	% number up to 999
	\else\ifnum#1#2<1000
		% the number of hundreds
		% TODO: if it is at the beginning of the word, [ein] may be left out in
		% a more informal language
		\ifnum#1>0 \decardinalnumberones{#1}\fi%
		hundert%
		\ifnum#2>0 \decardinalnumber{#2}\fi
		% TODO: 101 can be written as [ein]hundert[und]eins
		% [ein] and [und] are both optional
		% How should they be handled?
		%     (user has to specift somewhere if he likes them or not)
		% I also don't know for which numbers [und] is optional and
		% for which ones forbidden
	\else
		[$\ge$ 1000]%
	\fi\fi\fi\fi}

\defineconversion [decardinalnumber]    [\decardinalnumber]

\protect

\doifnotmode{demo} \endinput

\starttext

\encardinalnumber{-1}\par
\encardinalnumber{0}\par
\encardinalnumber{101}\par
\encardinalnumber{1001}\par
\encardinalnumber{1000001}\par
\encardinalnumber{1200021}

\stoptext
