zoukankan      html  css  js  c++  java
  • 【原】c#实现数字金额转换成大写金额

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace RMBTest
    {
        class Program
        {
            public static string[] MyScale = { "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾", "佰", "仟" };
            public static string[] MyScale2 = { "分", "角" };
            public static string[] MyBase = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
            static void Main(string[] args)
            {
                while (true)
                {
                    Console.WriteLine("请输入你要转换的金额:");
                    string sMoney = Console.ReadLine();
                    string result = "";
                   
                    try
                    {
                        double dm = double.Parse(sMoney);
                        string[] array = dm.ToString().Split('.');
                        int n = 0;
                        char[] cYuan;
                        char[] cJiao;
                        if (array.Length == 2)
                        {
                            if (array[1].Length > 2)
                            {
                                Console.WriteLine("你所输入的金额错误,无法转换,请重新输入!\n");
                                continue;
                            }
                            cYuan = array[0].ToString().ToCharArray();
                            n = cYuan.Length;
                            for (int i = 0; i < cYuan.Length; i++)
                            {
                                result += MyBase[Convert.ToInt32(cYuan[i].ToString())];
                                result += MyScale[Convert.ToInt32(n) - 1];
                                n--;
                            }

                            cJiao = array[1].ToString().ToCharArray();
                            n = cJiao.Length;
                            for (int i = 0; i < cJiao.Length; i++)
                            {
                                result += MyBase[Convert.ToInt32(cJiao[i].ToString())];
                                result += MyScale2[Convert.ToInt32(n) - 1];
                                n--;
                            }
                        }
                        else
                        {
                            cYuan = dm.ToString().ToCharArray();
                            n = cYuan.Length;
                            for (int i = 0; i < cYuan.Length; i++)
                            {
                                result += MyBase[Convert.ToInt32(cYuan[i].ToString())];
                                result += MyScale[Convert.ToInt32(n) - 1];
                                n--;
                            }
                        }
                        Console.WriteLine(result);
                    }
                    catch
                    {
                        Console.WriteLine("你所输入的金额错误,无法转换,请重新输入!\n");
                        continue;
                    }
                }
            }
        }
    }

  • 相关阅读:
    二分查找
    「数学」二次函数中项系数大小与图像的关系
    「数学」夹角公式
    「CF80A」Panoramix's Prediction
    「Luogu P6101」[EER2]出言不逊
    「数学」三角函数公式以及部分证明
    「Luogu P6069」[MdOI2020] Group
    「CF80B」Depression
    「数学」Menelaus定理与Ceva定理
    「AT1175」ニコニコ文字列
  • 原文地址:https://www.cnblogs.com/xsmhero/p/1728121.html
Copyright © 2011-2022 走看看