QT数字转英文

话不多说,直接上代码

enum Num
{
    negative = -1,
    zero = 0,
    one,
    two,
    three,
    four,
    five,
    six,
    seven,
    eight,
    nine,
    ten,
    eleven,
    twelve,
    thirteen,
    fourteen,
    fifteen,
    sisteen,
    seventeen,
    eighteen,
    nineteen,
    twenty,
    thirty = 30,
    forty = 40,
    fifty = 50,
    sisty = 60,
    seventy = 70,
    eighty = 80,
    ninety = 90,
    hundred = 100,
    thousand = 1000,
    million = 1000000,
    billion = 1000000000
};

QString MainWindow::numToEnglish(int nNum)
{
        QMap<Num, QString>mapNum;
        mapNum[negative] = "negative ";
        mapNum[zero] = "";
        mapNum[one] = "one ";
        mapNum[two] = "two ";
        mapNum[three] = "three ";
        mapNum[four] = "four ";
        mapNum[five] = "five ";
        mapNum[six] = "six ";
        mapNum[seven] = "seven ";
        mapNum[eight] = "eight ";
        mapNum[nine] = "nine ";
        mapNum[ten] = "ten ";
        mapNum[eleven] = "eleven ";
        mapNum[twelve] = "twelve ";
        mapNum[thirteen] = "thirteen ";
        mapNum[fourteen] = "fourteen ";
        mapNum[fifteen] = "fifteen ";
        mapNum[sisteen] = "sisteen ";
        mapNum[seventeen] = "seventeen ";
        mapNum[eighteen] = "eighteen ";
        mapNum[nineteen] = "nineteen ";
        mapNum[twenty] = "twenty ";
        mapNum[thirty] = "thirty ";
        mapNum[forty] = "forty ";
        mapNum[fifty] = "fifty ";
        mapNum[sisty] = "sisty ";
        mapNum[seventy] = "seventy ";
        mapNum[eighty] = "eighty ";
        mapNum[ninety] = "ninety ";
        mapNum[hundred] = "hundred ";
        mapNum[thousand] = "thousand ";
        mapNum[million] = "million ";
        mapNum[billion] = "billion ";
        QString strNum;
        int nTemp = abs(nNum);
        int nCarry = 1;
        QString strCarry;
        while(nTemp != 0)
        {
            int nLastThreeNum = nTemp % 1000;           // 最后三位bai数字
            int nHundredBit = nLastThreeNum / 100;      // 百位
            int nTenthBit = nLastThreeNum / 10 % 10;    // 十位du
            int nABit = nLastThreeNum % 10;             // 个位
            QString strTemp;
            strTemp.clear();
            if (nLastThreeNum == 0)
            {
                nTemp /= 1000;
                nCarry *= 1000;
                continue;
            }
            // 百位数字大于zhi0
            if (nHundredBit > 0)
            {
                strTemp += mapNum[(Num)nHundredBit];
                strTemp += mapNum[hundred];
            }
            // 十位数字大于2
            if (nTenthBit >= 2)
            {
                strTemp += mapNum[Num(nTenthBit*10)];
                strTemp += mapNum[Num(nABit)];
            }
            else if (nTenthBit == 1) // 十位数字为dao1
            {
                strTemp += mapNum[Num(nTenthBit*10+nABit)];
            }
            else if (nTenthBit == 0) // 十位数字为0
            {
                strTemp += mapNum[Num(nABit)];
            }

            // 进位表示
            strCarry = mapNum[Num(nCarry==1?0:nCarry)];
            strNum = strTemp + strCarry + strNum;
            nTemp /= 1000;
            nCarry *= 1000;
        }
        // 处理负数
        strNum = mapNum[Num(nNum<0?-1:0)] + strNum;
        //std::cout<
        return strNum;
}

 qDebug()<<"Num is "<<numToEnglish(55);   //Num is fifty five

你可能感兴趣的:(Qt,C++,Qt,Qt入门)