• 咱也写个小写数字转大写金额 ,纯粹字符串操作实现


    纯粹字符串操作实现

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

    namespace ConsoleApplication1
    {
        
    class Program
        
    {
            
    static void Main(string[] args)
            
    {

                Console.Write(
    "输入任意数字,回车转换");
                Console.Write(
    "\r\n");
                Console.Write(
    "直接回车则退出");
                Console.Write(
    "\r\n");

                
    for (; ; )
                
    {
                    
    string tmpString = Console.ReadLine();

                    
    if (tmpString.Trim() == string.Empty)
                    
    {
                        
    break;
                    }

                    
    try
                    
    {
                        Console.Write(
    string.Format("{0} \r\n", ConvertNumberToChineseNumber(tmpString)));
                    }

                    
    catch (Exception ex)
                    
    {
                        Console.Write(ex.Message);
                    }

                }


            }



            
    /// <summary>
            
    /// 数字转大写金额
            
    /// </summary>
            
    /// <param name="targetString">数字</param>
            
    /// <returns>大写金额</returns>

            private static string ConvertNumberToChineseNumber(string targetString)
            
    {
                
    string rtnValue = string.Empty;

                
    double tmpValue = 0;

                
    if (double.TryParse(targetString, out tmpValue))
                
    {
                    rtnValue 
    = tmpValue.ToString("#千兆#百兆#拾兆#兆#仟亿#百亿#拾亿#亿#千万#百万#拾万#万#仟#佰#拾#元.#角#分");
                }

                
    else
                
    {
                    
    throw new Exception("输入的不是数字或者过超出范围!");
                }


                
    //去个头
                int firstNumberIndex = rtnValue.IndexOfAny(new char[] '0''1''2''3''4''5''6''7''8''9' });

                
    if (firstNumberIndex >= 0)
                
    {
                    rtnValue 
    = rtnValue.Substring(firstNumberIndex);
                }



                
    //去小数点或去个尾
                int lastNumberIndex = rtnValue.LastIndexOfAny(new char[] '0''1''2''3''4''5''6''7''8''9' });

                rtnValue 
    = rtnValue.Substring(0, lastNumberIndex + 2);

                
    //小写大写切换

                rtnValue 
    = rtnValue.Replace(".""");
                rtnValue 
    = rtnValue.Replace("0""");
                rtnValue 
    = rtnValue.Replace("1""");
                rtnValue 
    = rtnValue.Replace("2""");
                rtnValue 
    = rtnValue.Replace("3""");
                rtnValue 
    = rtnValue.Replace("4""");
                rtnValue 
    = rtnValue.Replace("5""");
                rtnValue 
    = rtnValue.Replace("6""");
                rtnValue 
    = rtnValue.Replace("7""");
                rtnValue 
    = rtnValue.Replace("8""");
                rtnValue 
    = rtnValue.Replace("9""");

                
    return rtnValue;
            }

        }

    }
  • 相关阅读:
    LeetCode 260
    LeetCode 258
    LeetCode 237
    LeetCode 226
    LeetCode 203
    LeetCode 202
    codeforces 7D
    codefroces 7C
    codeforces 7B
    codeforces 6E (非原创)
  • 原文地址:https://www.cnblogs.com/sasbya/p/951987.html
Copyright © 2020-2023  润新知