zoukankan      html  css  js  c++  java
  • 人民币大小写转换

    代码
    public class DigitalToChinese
    {
        
    public DigitalToChinese()
        {
            
    //
            
    //TODO: 在此处添加构造函数逻辑
            
    //
        }
        
    public static string Convert(double digital)
        {
            
    if (digital == 0.00f)
                
    return "零元整";

            
    string buf = "";                        /**//* 存放返回结果 */
            
    string strDecPart = "";                    /**//* 存放小数部分的处理结果 */
            
    string strIntPart = "";                    /**//* 存放整数部分的处理结果 */

            
    /**/
            
    /* 将数据分为整数和小数部分 */
            
    char[] cDelim = { '.' };
            
    string[] tmp = null;
            
    string strDigital = digital.ToString();
            
    if (strDigital[0== '.')
            {                
    /**//* 处理数据首位为小数点的情况 */
                strDigital 
    = "0" + strDigital;
            }
            tmp 
    = strDigital.Split(cDelim, 2);

            
    /**/
            
    /* 整数部分的处理 */
            
    if (tmp[0].Length > 15)
            {
                
    throw new Exception("数值超出处理范围。");
            }
            
    bool flag = true;        /**//* 标示整数部分是否为零 */
            
    if (digital >= 1.00f)
            {
                strIntPart 
    = ConvertInt(tmp[0]);
                flag 
    = false;
            }

            
    /**/
            
    /* 存在小数部分,则处理小数部分 */
            
    if (tmp.Length == 2)
                strDecPart 
    = ConvertDecimal(tmp[1], flag);
            
    else
                strDecPart 
    = "";

            buf 
    = strIntPart + strDecPart;
            
    return buf;
        }


        
    private static string ConvertInt(string intPart)
        {
            
    string buf = "";
            
    int length = intPart.Length;
            
    int curUnit = length;

            
    /**/
            
    /* 处理除个位以上的数据 */
            
    string tmpValue = "";                    /**//* 记录当前数值的中文形式 */
            
    string tmpUnit = "";                    /**//* 记录当前数值对应的中文单位 */
            
    int i;
            
    for (i = 0; i < length - 1; i++, curUnit--)
            {
                
    if (intPart[i] != '0')
                {
                    tmpValue 
    = DigToCC(intPart[i]);
                    tmpUnit 
    = GetUnit(curUnit - 1);
                }
                
    else
                {
                    
    /**/
                    
    /* 如果当前的单位是"万、亿",则需要把它记录下来 */
                    
    if ((curUnit - 1% 4 == 0)
                    {
                        tmpValue 
    = "";
                        tmpUnit 
    = GetUnit(curUnit - 1);
                    }
                    
    else
                    {
                        tmpUnit 
    = "";
                        
    /**/
                        
    /* 如果当前位是零,则需要判断它的下一位是否为零,再确定是否记录'零' */
                        
    if (intPart[i + 1!= '0')
                        {
                            tmpValue 
    = "";
                        }
                        
    else
                        {
                            tmpValue 
    = "";
                        }
                    }
                }
                buf 
    += tmpValue + tmpUnit;
            }

            
    /**/
            
    /* 处理个位数据 */
            
    if (intPart[i] != '0')
                buf 
    += DigToCC(intPart[i]);
            buf 
    += "";

            
    return buf;
        }

        
    /// <summary>
        
    /// 小数部分的处理
        
    /// </summary>
        
    /// <param name="decPart">需要处理的小数部分</param>
        
    /// <param name="flag">标示整数部分是否为零</param>
        
    /// <returns></returns>
        private static string ConvertDecimal(string decPart, bool flag)
        {
            
    string buf = "";
            
    if ((decPart[0== '0'&& (decPart.Length > 1))
            {
                
    if (flag == false)
                {
                    buf 
    += "";
                }
                
    if (decPart[1!= '0')
                {
                    buf 
    += DigToCC(decPart[1]) + "";
                }
            }
            
    else
            {
                buf 
    += DigToCC(decPart[0]) + "";
                
    if ((decPart.Length > 1&& (decPart[1!= '0'))
                {
                    buf 
    += DigToCC(decPart[1]) + "";
                }
            }
            buf 
    += "";
            
    return buf;
        }


        
    /**/
        
    /// <summary>
        
    /// 获取人民币中文形式的对应位置的单位标志
        
    /// </summary>
        
    /// <param name="n"></param>
        
    /// <returns></returns>
        private static string GetUnit(int n)
        {
            
    switch (n)
            {
                
    case 1:
                    
    return "";
                
    case 2:
                    
    return "";
                
    case 3:
                    
    return "";
                
    case 4:
                    
    return "";
                
    case 5:
                    
    return "";
                
    case 6:
                    
    return "";
                
    case 7:
                    
    return "";
                
    case 8:
                    
    return "亿";
                
    case 9:
                    
    return "";
                
    case 10:
                    
    return "";
                
    case 11:
                    
    return "";
                
    case 12:
                    
    return "";
                
    case 13:
                    
    return "";
                
    case 14:
                    
    return "";
                
    case 15:
                    
    return "";
                
    default:
                    
    return " ";
            }
        }



        
    /**/
        
    /// <summary>
        
    /// 数字转换为相应的中文字符 ( Digital To Chinese Char )
        
    /// </summary>
        
    /// <param name="c">以字符形式存储的数字</param>
        
    /// <returns></returns>
        private static string DigToCC(char c)
        {
            
    switch (c)
            {
                
    case '1':
                    
    return "";
                
    case '2':
                    
    return "";
                
    case '3':
                    
    return "";
                
    case '4':
                    
    return "";
                
    case '5':
                    
    return "";
                
    case '6':
                    
    return "";
                
    case '7':
                    
    return "";
                
    case '8':
                    
    return "";
                
    case '9':
                    
    return "";
                
    case '0':
                    
    return "";
                
    default:
                    
    return "  ";
            }
        }

    }
  • 相关阅读:
    Asp.net MVC 利用 Nopi 导出 Excel
    React 中 调用 Asp.net WebApi
    Node.js mysql 连接池使用事务自动回收连接
    __far和__near的小问题
    注册博客园了,以后在这里写写随笔。
    Electron客户端开发入门必备的知识点
    公司组织构架的三大类型
    经济学中的人性抉择(下)
    经济学中的人性抉择(上)
    模拟音乐播放器播放条样式
  • 原文地址:https://www.cnblogs.com/ycsfwhh/p/1942020.html
Copyright © 2011-2022 走看看