zoukankan      html  css  js  c++  java
  • 咱也写个小写数字转大写金额 ,纯粹字符串操作实现

    纯粹字符串操作实现

    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;
            }

        }

    }
  • 相关阅读:
    使用cout进行格式化
    20175324 《Java程序设计》第3周学习总结
    20175324第二周学习总结
    第三周学习总结
    JAVA第二周学习总结
    20175330第一周学习总结。
    20175330第一周学习总结
    指针
    数组总结(一)
    数组练习题A财务管理
  • 原文地址:https://www.cnblogs.com/sasbya/p/951987.html
Copyright © 2011-2022 走看看