num2eng

static str num2eng(real _num)
{
    str     ret, sign, sCents = '';
    int     tThousand, i;
    str     sThousand[8];
//----------------------------------------------------------
    str cents2eng(real _cents)
    {
        if (_cents > 0)
            return strfmt(' And %1/100 Only.', (_cents * 100 mod 100));
        else
            return ' Only.';
    }
//----------------------------------------------------------
    str one2ninteen(int _teen)
    {
        str retStr;
        switch (_teen)
            {
            case 0 : retStr = ''; break;
            case 1 : retStr = 'One'; break;
            case 2 : retStr = 'Two'; break;
            case 3 : retStr = 'Three'; break;
            case 4 : retStr = 'Four'; break;
            case 5 : retStr = 'Five'; break;
            case 6 : retStr = 'Six'; break;
            case 7 : retStr = 'Seven'; break;
            case 8 : retStr = 'Eight'; break;
            case 9 : retStr = 'Nine'; break;
            case 10 : retStr = 'Ten'; break;
            case 11 : retStr = 'Eleven'; break;
            case 12 : retStr = 'Twelve'; break;
            case 13 : retStr = 'Thirteen'; break;
            case 14 : retStr = 'Fourteen'; break;
            case 15 : retStr = 'Fifteen'; break;
            case 16 : retStr = 'Sixteen'; break;
            case 17 : retStr = 'Seventeen'; break;
            case 18 : retStr = 'Eighteen'; break;
            case 19 : retStr = 'Ninteen'; break;
            default : retStr = ' Error ';
            }
        return retStr;
    }
//----------------------------------------------------------
    str tens(int _number)
    {
        str retStr;

        switch (_number)
            {
            case 0, 1 : retStr = one2ninteen(_number); break;
            case 2 : retStr = 'Twenty'; break;
            case 3 : retStr = 'Thirty'; break;
            case 4 : retStr = 'Fourty'; break;
            case 5 : retStr = 'Fifty'; break;
            case 6 : retStr = 'Sixty'; break;
            case 7 : retStr = 'Seventy'; break;
            case 8 : retStr = 'Eighty'; break;
            case 9 : retStr = 'Ninty'; break;
            default : retStr = ' Error ';
            }
        return retStr;
    }
//----------------------------------------------------------
    str thousand2eng(int _thousand)
    {
        str retStr;
        int tNum;

        tNum = _thousand div 100;
        if (tNum == 0)
            retStr = '';
        else if (tNum == 1)
            retStr = ' ' + one2ninteen(tNum) + ' Hundred';
        else
            retStr = ' ' + one2ninteen(tNum) + ' Hundreds';

        _thousand = _thousand mod 100;
        if (_thousand == 0)
            return retStr;

        tNum = _thousand div 10;
        if ((tNum == 0) || (tNum == 1))
            retStr += ' ' + one2ninteen(_thousand);
        else
            {
            retStr += ' ' + tens(tNum);
            if ((_thousand mod 10) != 0) retStr += '-';
            retStr += one2ninteen(_thousand mod 10);
            }
        return retStr;
    }
//----------------------------------------------------------

    sThousand[1] = '';
    sThousand[2] = '';
    sThousand[3] = ' Thousand';
    sThousand[4] = ' Thousands';
    sThousand[5] = ' Million';
    sThousand[6] = ' Millions';
    sThousand[7] = ' Billion';
    sThousand[8] = ' Billions';
    i = 1;

    if (_num > 200000000000)
        return 'number too big!';

    if ((_num div 1) == 0)
        ret = ' Zero';

    if (_num < 0)
        {
        sign = ' Credit';
        _num = abs(_num);
        }
    else
        sign = '';

    sCents = cents2eng(frac(_num));

    _num = _num div 1;
    while (_num > 0)
        {
        tThousand = _num mod 1000;
        if (tThousand == 1)
            {
            ret = sThousand[i*2-1] + ret;
            ret = thousand2eng(tThousand) + ret ;
            }
        else if (tThousand > 1)
            {
            ret = sThousand[i*2] + ret;
            ret = thousand2eng(tThousand) + ret ;
            }
        _num = _num / 1000 div 1;
        i++;
        }
    return (sign + ret + sCents);

}

posted @ 2012-03-01 11:59  perock  阅读(293)  评论(0编辑  收藏  举报