• 阿拉伯数字转中文小写数字


    C#版本

    View Code
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace NumberConventer
    {
        class Class1
        {
            private string[] CnNum = { "", "", "", "", "", "", "", "", "", "" };
            private string[] ShiNum = { "", "", "", "" };
            private string[] WanNum = { "", "", "亿" };
            private string NumTh, OutCn;
    
    
            private bool isNumber(string aaa)
            {
                try
                {
                    int bbb = Convert.ToInt16(aaa);
                    return true;
                }
                catch
                {
                    return false;
                }
            }//判断是否为数字
    
            private string ChangeChineseNum(string Number,int Numlan)
            {
                bool Zero = false, Wan = false, Hand = true;
                //zero 为是否需要输出汉字“零”,Wan为首否需要输出汉字“万”或“亿”,Hand 判断是否为第一个非零数字。
                int NumTemp;
                bool Booltemp = Int32.TryParse(Number, out NumTemp);
                if (!Booltemp)
                {
                    NumTemp = 0;
                }
                for (int i = 1; i <= Numlan; i++)
                {
                    NumTh = Number.Substring(i - 1, 1);
                    int NowNum, NowTh = Numlan + 1 - i;
    
                    if (isNumber(NumTh))
                    {
                        NowNum = Convert.ToInt16(NumTh);
                        //OutCn = OutCn + CnNum[i];
                    }
                    else
                    {
                        break;
                    }
    
                    if (NowNum == 0)
                    {
                        if (Hand == true)
                        {
                            continue;
                        }
                        else
                        {
                            Zero = true;
                        }
                    }
                    else
                    {
                        Wan = true;
                        if (Hand == true)
                        {
                            if (NumTemp > 9 && NumTemp < 20)
                            {
                                OutCn = OutCn + ShiNum[NowTh % 4];
                            }
                            else
                            {
                                OutCn = OutCn + CnNum[NowNum] + ShiNum[NowTh % 4];
                            }
    
                            Hand = false;
                        }
                        else
                        {
                            if (Zero == false)
                            {
                                OutCn = OutCn + CnNum[NowNum] + ShiNum[NowTh % 4];
                            }
                            else
                            {
                                OutCn = OutCn + CnNum[0] + CnNum[NowNum] + ShiNum[NowTh % 4];
                                Zero = false;
                            }
                        }
                    }
                    if (Wan == true && (NowTh - 1) % 4 == 0)
                    {
                        OutCn = OutCn + WanNum[((NowTh - 1) / 4) % 2];
                        for (int j = (NowTh - 1) / 8; j > 0; j = j - 1)
                        {
                            OutCn = OutCn + WanNum[2];
                        }
                        Wan = false;
                    }
    
                }
                if (isNumber(NumTh))
                {
                    return OutCn;
                }
                else
                {
                   return "包含非阿拉伯字符!请更正!";
                }
            }
        }
    }

    javascript版本

    View Code
    function ChangeChineseNum(Number) {
        var isNumber = function (aaa) {
            try {
                parseInt(aaa);
                return true;
            }
            catch (err) {
                return false;
            }
        };
    
        var CnNum = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
        var ShiNum = ["千", "", "十", "百"];
        var WanNum = ["", "万", "亿"];
        var NumTh, OutCn = "";
    
        var Zero = false, Wan = false, Hand = true;
        //zero 为是否需要输出汉字“零”,Wan为首否需要输出汉字“万”或“亿”,Hand 判断是否为第一个非零数字。
    
        var Numlan;
        var NumTemp;
        var Booltemp;
        if (isNumber(Number)) {
            NumTemp = parseInt(Number);
            Numlan = NumTemp.toString().length;
        }
        else {
            NumTemp = 0;
            Numlan = 0;
        }
        for (var i = 1; i <= Numlan; i++) {
            NumTh = Number.toString().substring(i - 1, i);
            var NowNum, NowTh = Numlan + 1 - i;
    
            if (isNumber(NumTh)) {
                NowNum = parseInt(NumTh);
                //OutCn = OutCn + CnNum[i];
            }
            else {
                break;
            }
    
            if (NowNum == 0) {
                if (Hand == true) {
                    continue;
                }
                else {
                    Zero = true;
                }
            }
            else {
                Wan = true;
                if (Hand == true) {
                    if (NumTemp > 9 && NumTemp < 20) {
                        OutCn = OutCn + ShiNum[NowTh % 4];
                    }
                    else {
                        OutCn = OutCn + CnNum[NowNum] + ShiNum[NowTh % 4];
                    }
    
                    Hand = false;
                }
                else {
                    if (Zero == false) {
                        OutCn = OutCn + CnNum[NowNum] + ShiNum[NowTh % 4];
                    }
                    else {
                        OutCn = OutCn + CnNum[0] + CnNum[NowNum] + ShiNum[NowTh % 4];
                        Zero = false;
                    }
                }
            }
            if (Wan == true && (NowTh - 1) % 4 == 0) {
                OutCn = OutCn + WanNum[((NowTh - 1) / 4) % 2];
                for (var j = parseInt((NowTh - 1) / 8); j > 0; j = j - 1) {
                    OutCn = OutCn + WanNum[2];
                }
                Wan = false;
            }
    
        }
        if (isNumber(NumTh)) {
            return OutCn;
        }
        else {
            return "包含非阿拉伯字符!请更正!";
        }
    }
  • 相关阅读:
    简单项目却持着一颗纠结的心
    一个简单的打的系统
    ftqghkwiNdrpid快速建项Sql语句生
    3/21 Django框架 模板路径及模板过滤器 1.模板路径查找
    3/19 Django框架 url路由配置及模板渲染
    3/18 Django框架 启动django服务
    3/16 Django框架 环境搭建
    Windows子系统配置,cmder配置, pycharm配置django开发环境
    Requests+正则表达式抓取猫眼电影TOP100
    分析Ajax抓取今日头条街拍美图
  • 原文地址:https://www.cnblogs.com/fumj/p/2933021.html
Copyright © 2020-2023  润新知