zoukankan      html  css  js  c++  java
  • 数字转汉字

    全局常量的声明

    const char * chnNumChar [10] = {"", "", "", "", "", "", "", "", "", ""};
    const char * sectionChar [4] = {"", "", "亿", "万亿"};
    const char * weightChar [4] = {"", "", "", ""};
    
    typedef long long ILONG;

    转化四位及其四位以下的整数

    string section2chn(ILONG onum)
    {
        string  res("");
        int weightindex = 0;
        bool iszero = false;
        while (onum > 0)
        {
            if (onum % 10)
                res = string(chnNumChar[onum % 10]) + weightChar[weightindex] + (iszero?chnNumChar[0]:"") + res;
            else if (!res.empty())
                iszero = true;
            ++ weightindex;
            onum /= 10;
        }
        return res;
    }

    转化整数

    string num2chn(ILONG onum)
    {
        string  res("");
        int sectionindex = 0;
        bool isneedzero = false;
        while (onum > 0)
        {
            if (onum % 10000)
                res = section2chn(onum % 10000) + sectionChar[sectionindex] + (isneedzero?chnNumChar[0]:"") + res;
            else if (!res.empty())
                isneedzero = true;
    
            if (onum % 10000 < 1000)
                isneedzero = true;
            ++ sectionindex;
            onum /= 10000;
        }
        return res;
    }

    测试

    #include <iostream>
    #include <string>
    #include <random>
    
    
    int main()
    {
        ILONG temnum = 6000000181;
        cout
            << temnum
            << "		"
            << num2chn(temnum)
            << endl;
        
        random_device rd;
        for (int i = 0; i < 1000; ++ i)
        {
            ILONG temnum = rd() ;
            cout
                << temnum
                << "		"
                << num2chn(temnum)
                << endl;
        }
    
        return 0;
    }
    出自datakv
  • 相关阅读:
    一个数组找出第k大的数(待补)
    变动二叉树
    判断一个二叉树
    Redis的过期策略和内存淘汰机制
    sql连接详解
    http 请求和格式
    java基础知识
    分页信息
    持续集成之Jenkins自动部署war包到远程服务器
    no-sql数据库之redis
  • 原文地址:https://www.cnblogs.com/datakv/p/5606229.html
Copyright © 2011-2022 走看看