function theResult = int2english(x) % int2english -- English name of an integer. % int2english(x) returns or displays the English % name of x, an integer, up to about 10^22. % int2english('demo') demonstrates itself with % a random integer. % Copyright (C) 1999 Dr. Charles R. Denham, ZYDECO. % All Rights Reserved. % Disclosure without explicit written consent from the % copyright owner does not constitute publication. % Version of 29-Dec-1999 05:55:08. % Updated 29-Dec-1999 07:36:13. if nargout > 0, theResult = []; end if nargin < 1, x = 'demo'; end if isequal(x, 'demo') help(mfilename) u = fix(rand(1, 1) * 100000); v = feval(mfilename, u); disp([' ## ' mfilename '(' int2str(u) ') = ' v]) return end if ischar(x), x = eval(x); end theNames = { 'zero' 'thousand' 'million' 'billion' 'trillion' 'quadrillion' 'quintillion' 'sextillion' 'septillion' 'octtillion' 'nonillion' 'decillion' }; x = fix(x); isNegative = x < 0; x = abs(x); y = ''; if x < 20 y = do1(x); elseif x < 100 y = do2(x); else k = 0; while x > 0 a = rem(x, 1000); if a > 999 disp([' ## Too big for Matlab "rem": ' num2str(x)]) break elseif a > 0 temp = do3(a); if k > 0 temp = [temp ' ' theNames{k+1}]; end if ~isempty(y), y = [' ' y]; end y = [temp y]; end k = k+1; x = fix(x/1000); end end if isNegative, y = ['minus ' y]; end if nargout > 0 theResult = y; else disp(y) end % ---------- do3 ---------- % function y = do3(x) % do3 -- Name of an integer < 1000. % do3(x) returns the name of integer x < 1000. if x < 100 y = do2(x); else a = fix(x/100); b = rem(x, 100); y = [do1(a) ' hundred']; if b > 0 y = [y ' ' do2(b)]; end end % ---------- do2 ---------- % function y = do2(x) % do2 -- Name of an integer < 100. % do2(x) returns the name of integer x < 100. theNames = { 'zero' 'ten' 'twenty' 'thirty' 'forty' 'fifty' 'sixty' 'seventy' 'eighty' 'ninety' }; if x < 20 y = do1(x); else a = fix(x/10); b = rem(x, 10); y = theNames{a+1}; if b > 0 y = [y ' ' do1(b)]; end end % ---------- do1 ---------- % function y = do1(x) % do1 -- Name of an integer < 20. % do1(x) returns the name of integer x < 20. theNames = { 'zero' 'one' 'two' 'three' 'four' 'five' 'six' 'seven' 'eight' 'nine' 'ten' 'eleven' 'twelve' 'thirteen' 'fourteen' 'fifteen' 'sixteen' 'seventeen' 'eighteen' 'nineteen' }; y = theNames{x+1};