• 数字大小写转换的实现


    今天上午上课无聊,写了个数字大小写转换的程序,能转换千亿以内的任何数值,代码见下,幼稚了,让大家见笑了。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace NumberConver
    {
        class Program
        {
            static void Main(string[] args)
            {
                while (true)
                {
                    //执行转换功能
                    compute();
                    //换行
                    Console.WriteLine();
                }
            }
            /// <summary>
            
    /// 执行转换功能
            
    /// </summary>
            static void compute()
            {
                Console.Write("请输入需要转换的数字:");
                //获取输入值
                string num = Console.ReadLine().Trim();
                //清除首末的0
                num = clean(num);
                //判断是否为空
                if (String.IsNullOrWhiteSpace(num))
                {
                    Console.WriteLine("数字不能为空或者为空格。");
                    //重新输入
                    compute();
                }
                //检查输入
                if (checkStandard(num))
                {
                    //加个小数点
                    num += ".";
                    //小数点左边值
                    string left = num.Split('.')[0];
                    //小数点右边值
                    string right = num.Split('.')[1];
                    //单位符号数组
                    string[] symbol = { """""""""""""""""亿""""""" };
                    //大小写数组
                    string[] uppercase = { """""""""""""""""""" };
                    //输出结果
                    string result = "";
                    //标识是否是中间0
                    int middlezero = 0;
                    //左边转换为字符数组
                    char[] cLeft = left.ToCharArray();
                    //如果整数部分只是0
                    if (cLeft[0] == '0')
                    {
                        //直接零开头
                        result += "";
                    }
                    //计算整数(左边)部分
                    for (int i = 0; i < cLeft.Length; i++)
                    {
                        //如果是中间位的0
                        if (cLeft[i] != '0')
                        {
                            //转换为大写
                            result += uppercase[int.Parse(cLeft[i].ToString())];
                            //添加单位
                            result += symbol[cLeft.Length - 1 - i];
                            //中间0重置
                            middlezero = 0;
                        }
                        else
                        {
                            //如果不是整数的最末位是0
                            if (i != cLeft.Length - 1)
                            {
                                //如果下一位是非0
                                if (cLeft[i + 1] != '0' && (cLeft.Length - 1 - i != 4 && cLeft.Length - 1 - i != 8))
                                {
                                    //转换为大写
                                    result += uppercase[int.Parse(cLeft[i].ToString())];
                                }
                                //如果下一位依然是0
                                else
                                {
                                    //如果是万位置后面的最后0
                                    if (cLeft.Length - 1 - i == 4 && middlezero < 3)
                                    {
                                        //添加万位符
                                        result += "";
                                        //中间0结束先
                                        middlezero = 0;
                                        //如果下一位首位为0
                                        if (cLeft[i + 1] == '0')
                                        {
                                            result += "";
                                            i++;
                                        }
                                    }
                                    //如果是亿后面的0
                                    if (cLeft.Length - 1 - i == 8 && middlezero < 3)
                                    {
                                        //添加亿位符
                                        result += "亿";
                                        //中间0结束
                                        middlezero = 0;
                                        //如果下一位首位为0
                                        if (cLeft[i + 1] == '0')
                                        {
                                            result += "";
                                            i++;
                                        }
                                    }
                                }
                            }
                            middlezero++;
                        }
                    }
                    //小数部分如果不为空
                    if (right.Trim() != "")
                    {
                        //添加小数点
                        result += "";
                        //左边转换为字符
                        char[] cRight = right.ToCharArray();
                        //计算右边小数部分
                        for (int i = 0; i < cRight.Length; i++)
                        {
                            //添加大写部分
                            result += uppercase[int.Parse(cRight[i].ToString())];
                        }
                    }
                    //输出转换后的结果
                    Console.WriteLine(result);
                }
                //非正常情况
                else
                {
                    //此处可不写
                }
            }
            /// <summary>
            
    /// 检查输入字符是否满足规范
            
    /// </summary>
            static bool checkStandard(string input)
            {
                //临时变量
                int temp = 0;
                //第一步,判断小数点小于等于1个
                int point = 0;
                //第二步,全部都是数字
                int otherChar = 0, num = 0;
                //第三步,小数点整数部分限定最大值为12位
                int left = 0;
                //首先全部转换为字符数组
                char[] myInput = input.ToCharArray();
                //遍历
                for (int i = 0; i < myInput.Length; i++)
                {
                    //如果不是点,我们就当作数字字符
                    if (myInput[i] != '.')
                    {
                        //能成功转换为数字
                        if (int.TryParse(myInput[i].ToString(), out temp))
                        {
                            //数字+1
                            num++;
                            //如果是小数点左边
                            if (point < 1)
                            {
                                //表示当前是左边数字
                                left++;
                            }
                        }
                        //其它字符
                        else
                        {
                            //其它字符+1
                            otherChar++;
                        }
                    }
                    //是小数点
                    else
                    {
                        //小数点+1
                        point++;
                    }
                }
                //如果小数点多余1个
                if (point > 1)
                {
                    Console.WriteLine("您的输入中有多余的小数点!");
                    return false;
                }
                //正常情况下
                else
                {
                    //如果左边的整数部分超过12位
                    if (left > 12)
                    {
                        Console.WriteLine("整数部分溢出,整数部分不得超过12位!");
                        return false;
                    }
                    //正常情况下
                    else
                    {
                        //是否存在其它字符
                        if (otherChar > 0)
                        {
                            Console.WriteLine("您的输入存在非数字字符,请仔细检查!");
                            return false;
                        }
                        //一切正常
                        else
                        {
                            return true;
                        }
                    }
                }
            }
            /// <summary>
            
    /// 清除首部的0和末尾的0
            
    /// </summary>
            
    /// <param name="input">输入的字符串</param>
            
    /// <returns>返回清除后的字符串</returns>
            static string clean(string input)
            {
                //首先清空字符首末的空格符
                input=input.Trim();
                //如果以0开头
                while (input.StartsWith("0"))
                {
                    //把0清除
                    input = input.Substring(1, input.Length - 1);
                }
                //如果是小数点的0结尾
                while (input.EndsWith("0") && input.Contains("."))
                {
                    //清楚末尾的0
                    input = input.Substring(0, input.Length - 1);
                }
                //如果开始是小数点
                if (input.StartsWith("."))
                {
                    //前面加0
                    input = "0" + input;
                }
               
                //返回Input
                return input;
            }
        }
    }
     
  • 相关阅读:
    tomcat使用redis存储共享session
    dubbo专题」dubbo其实很简单,就是一个远程服务调用的框架(1)
    SpringBoot Test集成测试
    IntelliJ Idea设置护眼浅绿色背景方法
    IDEA使用有道翻译插件
    IDEA Translation插件中有道智云(有道翻译)应用ID,密钥申请教程
    利用grep-console插件使Intellij idea显示多颜色调试日志
    mybatis 整合spring之mapperLocations配置的问题
    idea将maven项目打包成war包的方式,以及使用war包
    maven 常用命令
  • 原文地址:https://www.cnblogs.com/liufanping/p/2414188.html
Copyright © 2020-2023  润新知