No BSD License  

Highlights from
num2english

from num2english by Dave Kellow
Translates digits to english

num2english(b,arg)
function engb = num2english(b,arg)
%  NUM2ENGLISH Convert numbers to plain english.
%     T = num2english(X) returns the plain english representation of the
%     number X. If X is a vector, it returns a character array with each
%     number in a different row.
%
%     T = num2english(X,'year') returns a string sounding more like a year,
%     for instance num2english(1984,'year') will return 'nineteen
%     eighty-four'. If the third digit is a zero it will return 'oh', so
%     num2english(1706,'year') returns 'seventeen oh-six'.
%
%     T = num2english(X,'th') returns first, second, third etc. instead of
%     one two three.
%
%  Source: Dave Kellow, kellowd@mar.dfo-mpo.gc.ca
%


format long;

engb = [];
for nlq = 1:length(b)
    a = b(nlq);


    neg_sect = [];
    if a<0
        neg_sect = ['negative '];
        a = abs(a);
    end
    if nargin==1
        arg = [];
    end

    if strcmp(arg,'year')
        if ~isempty(neg_sect)
            error('No such thing as a negative year. ');
        end
        stryear = num2str(fix(a));
        if length(stryear)>2
            firstpart = zero2999(str2num(stryear(1:end-2)));


            secondpart = zero2999(str2num(stryear(end-1:end)));
            if str2num(stryear(end-1:end))>0 && str2num(stryear(end-1:end))<10
                secondpart = ['oh-',secondpart];
            end


            engy = [firstpart,' ',secondpart];
        end
    end
    if ~strcmp(arg,'th')
        th = 0;
    else
        th = 1;
    end

    if strcmp(arg,'example');
        clc;
        lumberjacks = 100237654
        pancakes = 27.5
        day_of_march = 25
        year = 1788
        seconds_per_tree = 0.0876567421
        disp([num2english(lumberjacks),' lumberjacks ate ',num2english(pancakes),' pancakes on the ',...
            num2english(day_of_march,'th'),' day of march in the year ',num2english(year,'year'),'. It takes ',...
            num2english(seconds_per_tree),' seconds to cut down a tree.']);
        return;
    end





    sing = {'zero','one','two','three','four','five','six','seven','eight','nine'};
    teen = {'ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen'};
    doub = {'zero','ten','twenty','thirty','fourty','fifty','sixty','seventy','eighty','ninety'};
    hund = 'hundred';
    thou = 'thousand';
    mill = 'million';
    bill = 'billion';
    tril = 'trillion';
    hundth = 'hundredth';
    thouth = 'thousandth';
    millth = 'millionth';
    billth = 'billionth';
    trilth = 'trillionth';
    point = 'point';

    if a>(999999999999999)
        error('This utility doesn''t do quadrillions or higher. Yet.');
    end

    stra = num2str(a);
    qqq = 4;
    while str2num(stra)~=a
        qqq=qqq+1;
        stra = num2str(a,qqq);
    end


    pq = findstr('.',stra);
    if ~isempty(pq)
        if th
            error('Can''t do decimalth numbers; integers only.');
        end

        decimal_portion = [' ',point,' '];
        for l = pq+1:length(stra)
            decimal_portion = [decimal_portion,char(sing{str2num(stra(l))+1}),' '];
        end
        stra_int = stra(1:pq-1);
    else
        decimal_portion = [];
        stra_int = stra;
    end
    Q = length(stra_int);
    integer_portion=[];
    if Q>12
        integer_portion = [zero2999(str2num(stra_int(1:end-12))),' ',tril];
        if str2num(stra_int(end-11:end-2))>0
            integer_portion = [integer_portion,', '];
        elseif str2num(stra_int(end-1:end))>0
            integer_portion = [integer_portion,' '];
        else
            integer_portion = [integer_portion,' '];
        end
        if str2num(stra_int(end-11:end))==0 && th
            integer_portion = strrep(integer_portion,tril,trilth);
        end


    end
    if Q>9 && str2num(stra_int(max(1,end-11):end-9))
        integer_portion = [integer_portion, zero2999(str2num(stra_int(max(1,end-11):end-9))),' ',bill];
        if str2num(stra_int(end-8:end-2))>0
            integer_portion = [integer_portion,', '];
        elseif str2num(stra_int(end-1:end))>0
            integer_portion = [integer_portion,' '];
        else
            integer_portion = [integer_portion,' '];
        end
        if str2num(stra_int(end-8:end))==0 && th
            integer_portion = strrep(integer_portion,bill,billth);
        end

    end
    if Q>6 && str2num(stra_int(max(1,end-8):end-6))
        integer_portion = [integer_portion, zero2999(str2num(stra_int(max(1,end-8):end-6))),' ',mill];
        if str2num(stra_int(end-5:end-2))>0
            integer_portion = [integer_portion,', '];
        elseif str2num(stra_int(end-1:end))>0
            integer_portion = [integer_portion,' '];
        else
            integer_portion = [integer_portion,' '];
        end
        if str2num(stra_int(end-5:end))==0 && th
            integer_portion = strrep(integer_portion,mill,millth);
        end
    end
    if Q>3 && str2num(stra_int(max(1,end-5):end-3))>0
        integer_portion = [integer_portion, zero2999(str2num(stra_int(max(1,end-5):end-3))),' ',thou];
        if str2num(stra_int(end-2))>0
            integer_portion = [integer_portion,', '];
        elseif str2num(stra_int(end-1:end))>0
            integer_portion = [integer_portion,' '];
        else
            integer_portion = [integer_portion,' '];
        end
        if str2num(stra_int(end-2:end))==0 && th
            integer_portion = strrep(integer_portion,thou,thouth);
        end
    end

    if str2num(stra_int(max(1,end-2):end))>0
        integer_portion = [integer_portion,zero2999(str2num(stra_int(max(1,end-2):end)),th)];
    elseif str2num(stra_int)==0 && ~isempty(decimal_portion)
        integer_portion = [integer_portion,zero2999(str2num(stra_int(max(1,end-2):end)))];
    elseif str2num(stra_int)==0 && isempty(decimal_portion)
        integer_portion = 'zero ';
        if th
            integer_portion = 'zeroth';
        end
    end



    eng = [neg_sect, integer_portion,decimal_portion];
    eng = strrep(eng,'  ',' ');
    eng = deblank(eng);
    if strcmp(arg,'year')
        engy = strrep(engy,'zero','oh-oh');
        engb = str2mat(engb,engy);
    else
        engb = str2mat(engb,eng);
    end

end


function zero2999str = zero2999(num,th)
sing = {'zero','one','two','three','four','five','six','seven','eight','nine'};
singth = {'zeroth','first','second','third','fourth','fifth','sixth','seventh','eighth','ninth'};
teen = {'ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen'};
teenth = {'tenth','eleventh','twelfth','thirteenth','fourteenth','fifteenth','sixteenth','seventeenth','eighteenth','nineteenth'};
doub = {'zero','ten','twenty','thirty','fourty','fifty','sixty','seventy','eighty','ninety'};
doubth = {'zeroth','tenth','twentieth','thirtieth','fourtieth','fiftieth','sixtieth','seventieth','eightieth','ninetieth'};
hund = 'hundred';
thou = 'thousand';
mill = 'million';
bill = 'billion';
tril = 'trillion';
hundth = 'hundredth';
thouth = 'thousandth';
millth = 'millionth';
billth = 'billionth';
trilth = 'trillionth';
if nargin==1
    th = 0;
end



stra_int = num2str(num);
R = length(num2str(num));
switch R
    case 1
        hundred_portion = [];
        zero2999str = [sing{num+1}];
        if th
            zero2999str = [singth{num+1}];
        end

    case 2
        last_two = str2num(stra_int(1:2));
        if last_two==0
            zero2999str = [];
        elseif last_two<10 && last_two>0
            zero2999str = [sing{last_two+1}];
            if th
                zero2999str = [singth{last_two+1}];
            end

        elseif last_two<20 && last_two>9
            zero2999str = [teen{last_two-9}];
            if th
                zero2999str = [teenth{last_two-9}];
            end

        else
            last_two_str = num2str(last_two);
            last_two_tens = str2num(last_two_str(1));
            last_two_ones = str2num(last_two_str(2));
            last_two_tens_portion = doub{last_two_tens+1};
            if last_two_ones>0
                last_two_ones_portion = ['-',sing{last_two_ones+1}];
                if th
                    last_two_ones_portion = ['-',singth{last_two_ones+1}];
                end
            else
                last_two_ones_portion = [];
                if th
                    last_two_tens_portion = doubth{last_two_tens+1};
                end
            end
            zero2999str = [last_two_tens_portion,last_two_ones_portion];
        end
    case 3
        hundred_portion = [char(sing{str2num(stra_int(1))+1}),' ',hund];
        last_two = str2num(stra_int(2:3));
        if last_two==0
            zero2999str = [hundred_portion];
            if th
                zero2999str = [char(sing{str2num(stra_int(1))+1}),' ',hundth];
            end
        elseif last_two<10 && last_two>0
            zero2999str = [hundred_portion,' ',sing{last_two+1}];
            if th
                zero2999str = [hundred_portion,' ',singth{last_two+1}];
            end

        elseif last_two<20 && last_two>9
            zero2999str = [hundred_portion,' ',teen{last_two-9}];
            if th
                zero2999str = [hundred_portion,' ',teenth{last_two-9}];
            end

        else
            last_two_str = num2str(last_two);
            last_two_tens = str2num(last_two_str(1));
            last_two_ones = str2num(last_two_str(2));
            last_two_tens_portion = doub{last_two_tens+1};
            if last_two_ones>0
                last_two_ones_portion = ['-',sing{last_two_ones+1}];
                if th
                    last_two_ones_portion = ['-',singth{last_two_ones+1}];
                end
            else
                last_two_ones_portion = [];
                if th
                    last_two_tens_portion = doubth{last_two_tens+1};
                end
            end
            zero2999str = [hundred_portion,' ',last_two_tens_portion,last_two_ones_portion];
        end
end





Contact us at files@mathworks.com