1 C++阿拉伯数字字符串转换中文读法的中文字符串,不支持小数点

最近碰到一个这样的需求,就是将阿拉伯数字字符串转换为中文读法的中文字符串,形如将

"199922"

转换为:

"十九万九千九百二十二"

经过一番折腾,已经可以成功完成转换,并支持个位数到兆的范围转换,我的示例代码如下:

#include <iostream>
#include <string>

std::wstring ConvertNumToChineseNum(const std::wstring& num)
{
    std::wstring chinaeseNumResult;

    std::wstring num_copy = num;

    int size = num_copy.length();

    for (int i = 0; i < num_copy.size(); ++i)
    {
        if (num_copy[i] == L'1')
        {
            chinaeseNumResult += L"一";
        }
        else if (num_copy[i] == L'2')
        {
            chinaeseNumResult += L"二";
        }
        else if (num_copy[i] == L'3')
        {
            chinaeseNumResult += L"三";
        }
        else if (num_copy[i] == L'4')
        {
            chinaeseNumResult += L"四";
        }
        else if (num_copy[i] == L'5')
        {
            chinaeseNumResult += L"五";
        }
        else if (num_copy[i] == L'6')
        {
            chinaeseNumResult += L"六";
        }
        else if (num_copy[i] == L'7')
        {
            chinaeseNumResult += L"七";
        }
        else if (num_copy[i] == L'8')
        {
            chinaeseNumResult += L"八";
        }
        else if (num_copy[i] == L'9')
        {
            chinaeseNumResult += L"九";
        }

        if (num_copy[i] == L'0')
        {
            if (size % 4 == 3)
            {
                if (num_copy[i + 1] != L'0' || num_copy[i + 2] != L'0')
                {
                    chinaeseNumResult += L"零";
                }
            }
            else if (size % 4 == 2 && num_copy[i - 1] != L'0')
            {
                if (num_copy[i + 1] != L'0')
                {
                    chinaeseNumResult += L"零";
                }
            }
        }

        if (size % 4 == 1)
        {
            if (size / 4 == 1)
            {
                if (num_copy[i] != L'0' || num_copy[i - 1] != L'0' || num_copy[i - 2] != L'0' || num_copy[i - 3] != L'0')
                {
                    chinaeseNumResult += L"万";
                }
            }
            else if (size / 4 == 2)
            {
                if (num_copy[i] != L'0' || num_copy[i - 1] != L'0' || num_copy[i - 2] != L'0' || num_copy[i - 3] != L'0')
                {
                    chinaeseNumResult += L"亿";
                }
            }
            if (size / 4 == 3)
            {
                if (num_copy[i] != L'0' || num_copy[i - 1] != L'0' || num_copy[i - 2] != L'0' || num_copy[i - 3] != L'0')
                {
                    chinaeseNumResult += L"兆";
                }
            }

        }

        if (num_copy[i] != L'0')
        {
            if (size % 4 == 2)
            {
                chinaeseNumResult += L"十";
            }
            if (size % 4 == 3) 
            {
                chinaeseNumResult += L"百";
            }
            if (size % 4 == 0)
            {
                chinaeseNumResult += L"千";
            }
        }

        --size;
    }


    return chinaeseNumResult;
}


int main()
{
    std::wstring result = ConvertNumToChineseNum(L"352717183921");

    std::wcout << result << std::endl;

    return 0;
}

运行结果如下:

C++ – 阿拉伯数字字符串转换为中文读法的中文字符串,支持小数点-StubbornHuang Blog

2 C++阿拉伯数字字符串转换为中文读法的中文字符串,支持小数点

形如将

"199922.99"

转换为:

"十九万九千九百二十二点九九"

示例代码如下:

#include <iostream>
#include <string>

// 将阿拉伯字符串的整数部分,即小数点前的部分转换为中文数字字符串
std::wstring ConverArabicNumIntegerPartToChineseNum(const std::wstring& arab_num_integer_part)
{
    std::wstring chineseNumResult;

    std::wstring arab_num_integer_part_copy = arab_num_integer_part;

    int size = arab_num_integer_part_copy.length();

    for (int i = 0; i < arab_num_integer_part_copy.size(); ++i)
    {
        if (arab_num_integer_part_copy[i] == L'1')
        {
            chineseNumResult += L"一";
        }
        else if (arab_num_integer_part_copy[i] == L'2')
        {
            chineseNumResult += L"二";
        }
        else if (arab_num_integer_part_copy[i] == L'3')
        {
            chineseNumResult += L"三";
        }
        else if (arab_num_integer_part_copy[i] == L'4')
        {
            chineseNumResult += L"四";
        }
        else if (arab_num_integer_part_copy[i] == L'5')
        {
            chineseNumResult += L"五";
        }
        else if (arab_num_integer_part_copy[i] == L'6')
        {
            chineseNumResult += L"六";
        }
        else if (arab_num_integer_part_copy[i] == L'7')
        {
            chineseNumResult += L"七";
        }
        else if (arab_num_integer_part_copy[i] == L'8')
        {
            chineseNumResult += L"八";
        }
        else if (arab_num_integer_part_copy[i] == L'9')
        {
            chineseNumResult += L"九";
        }

        if (arab_num_integer_part_copy[i] == L'0')
        {
            if (size % 4 == 3)
            {
                if (arab_num_integer_part_copy[i + 1] != L'0' || arab_num_integer_part_copy[i + 2] != L'0')
                {
                    chineseNumResult += L"零";
                }
            }
            else if (size % 4 == 2 && arab_num_integer_part_copy[i - 1] != L'0')
            {
                if (arab_num_integer_part_copy[i + 1] != L'0')
                {
                    chineseNumResult += L"零";
                }
            }
        }

        if (size % 4 == 1)
        {
            if (size / 4 == 1)
            {
                if (arab_num_integer_part_copy[i] != L'0' || arab_num_integer_part_copy[i - 1] != L'0' || arab_num_integer_part_copy[i - 2] != L'0' || arab_num_integer_part_copy[i - 3] != L'0')
                {
                    chineseNumResult += L"万";
                }
            }
            else if (size / 4 == 2)
            {
                if (arab_num_integer_part_copy[i] != L'0' || arab_num_integer_part_copy[i - 1] != L'0' || arab_num_integer_part_copy[i - 2] != L'0' || arab_num_integer_part_copy[i - 3] != L'0')
                {
                    chineseNumResult += L"亿";
                }
            }
            if (size / 4 == 3)
            {
                if (arab_num_integer_part_copy[i] != L'0' || arab_num_integer_part_copy[i - 1] != L'0' || arab_num_integer_part_copy[i - 2] != L'0' || arab_num_integer_part_copy[i - 3] != L'0')
                {
                    chineseNumResult += L"兆";
                }
            }

        }

        if (arab_num_integer_part_copy[i] != L'0')
        {
            if (size % 4 == 2)
            {
                chineseNumResult += L"十";
            }
            if (size % 4 == 3)
            {
                chineseNumResult += L"百";
            }
            if (size % 4 == 0)
            {
                chineseNumResult += L"千";
            }
        }

        --size;
    }


    return chineseNumResult;
}

// 将阿拉伯字符串的小数部分,即小数点后的部分转换为中文数字字符串
std::wstring ConverArabicNumDecimalPartToChineseNum(const std::wstring& arab_num_decimal_part)
{
    std::wstring chineseNumResult;

    std::wstring arab_num_decimal_part_copy = arab_num_decimal_part;

    for (int i = 0; i < arab_num_decimal_part_copy.size(); ++i)
    {
        if (arab_num_decimal_part_copy[i] == L'0')
        {
            chineseNumResult += L"零";
        }
        else if (arab_num_decimal_part_copy[i] == L'1')
        {
            chineseNumResult += L"一";
        }
        else if (arab_num_decimal_part_copy[i] == L'2')
        {
            chineseNumResult += L"二";
        }
        else if (arab_num_decimal_part_copy[i] == L'3')
        {
            chineseNumResult += L"三";
        }
        else if (arab_num_decimal_part_copy[i] == L'4')
        {
            chineseNumResult += L"四";
        }
        else if (arab_num_decimal_part_copy[i] == L'5')
        {
            chineseNumResult += L"五";
        }
        else if (arab_num_decimal_part_copy[i] == L'6')
        {
            chineseNumResult += L"六";
        }
        else if (arab_num_decimal_part_copy[i] == L'7')
        {
            chineseNumResult += L"七";
        }
        else if (arab_num_decimal_part_copy[i] == L'8')
        {
            chineseNumResult += L"八";
        }
        else if (arab_num_decimal_part_copy[i] == L'9')
        {
            chineseNumResult += L"九";
        }
    }


    return chineseNumResult;
}

std::wstring ConvertArabicNumToChineseNum(const std::wstring& arab_num)
{
    std::wstring chineseNumResult;

    std::wstring arab_num_copy = arab_num;

    // 如果包含了小数点
    if (arab_num_copy.find(L".") != std::wstring::npos)
    {
        int dotPosition = arab_num_copy.find(L".");
        std::wstring integerPartNum = arab_num_copy.substr(0, dotPosition);
        std::wstring decimalPartNum = arab_num_copy.substr(dotPosition + 1);

        chineseNumResult += ConverArabicNumIntegerPartToChineseNum(integerPartNum);
        chineseNumResult += L"点";
        chineseNumResult += ConverArabicNumDecimalPartToChineseNum(decimalPartNum);
    }
    else
    {
        chineseNumResult += ConverArabicNumIntegerPartToChineseNum(arab_num_copy);
    }

    return chineseNumResult;
}


int main()
{
    std::wstring result = ConvertArabicNumToChineseNum(L"131346546511.99");

    std::wcout << result << std::endl;

    return 0;
}