当前位置:  编程技术>.net/c#/asp.net

NumberToUpper数字转中文详解

    来源: 互联网  发布时间:2014-10-25

    本文导语:  使用时需开启unsafe选项 构造函数有4个参数 number : 数字文本 isSimplified : 是否只使用简体中文,默认:false isMoney : 是否是金额模式(忽略小数点后3位,并加上单位"元,角,分或整"),否认:true veryBig : 是否开启大数字文本模式(接受15位以...

使用时需开启unsafe选项

构造函数有4个参数

number : 数字文本

isSimplified : 是否只使用简体中文,默认:false

isMoney : 是否是金额模式(忽略小数点后3位,并加上单位"元,角,分或整"),否认:true

veryBig : 是否开启大数字文本模式(接受15位以上整数,及10位以上小数),否认:false

代码如下:

using System;
using System.ComponentModel;
using System.Text;
using System.Text.RegularExpressions;

namespace blqw
{
    public static class NumberToUpper
    {
        #region 固定参数
        //^[-+]?d*(?[.])?d*$
        readonly static Regex CheckNumber = new Regex(@"^[st]*0*(?[-+]?d*)[.]?(?d*[1-9])?[0]*[st]*$", RegexOptions.Compiled);

        readonly static string[] UpperNumbers =
        {
            "零壹貳叁肆伍陸柒捌玖點",
            "零一二三四五六七八九点"
        };
        readonly static string[] NumberUnits =
        {
            "仟萬拾佰億負",
            "千万十百亿负"
        };
        readonly static string[] MoneyUnits =
        {
            "圓角分",
            "元角分"
        };

        #endregion

        /// 将数字文本转换成大写
        ///
        /// 数字文本
        /// 是否只使用简体中文,默认:否
        /// 是否是金额模式(忽略小数点后3位,并加上单位"元,角,分或整"),否认:是
        /// 是否开启大数字文本模式(接受15位以上整数,及10位以上小数),否认:否
        public static string Go(string number, bool isSimplified = false, bool isMoney = true, bool veryBig = false)
        {
            if (number == null)
            {
                throw new ArgumentNullException("number", "number不能为空");
            }
            //number = number.Trim(' ', 't');    //去掉多余的空格,制表符
            var m = CheckNumber.Match(number);
            if (m.Success == false)
            {
                throw new ArgumentException("number不是数字", "number");
            }

            unsafe
            {
                fixed (char* p = number)
                fixed (char* upnum = UpperNumbers[isSimplified.GetHashCode()])
                fixed (char* numut = NumberUnits[isSimplified.GetHashCode()])
                fixed (char* monut = MoneyUnits[isSimplified.GetHashCode()])
                {
                    var mdec = m.Groups["decimal"];
                    var mInt = m.Groups["integer"];
                    if (mInt.Length > 15 && veryBig == false)
                    {
                        throw new ArgumentException("不建议转换超过15位的整数,除非将veryBig参数设置为true", "number");
                    }
                    if (mdec.Length > 10 && veryBig == false)
                    {
                        throw new ArgumentException("不建议转换超过10位的小,除非将veryBig参数设置为true", "number");
                    }
                    string integer = ParseInteger(p + mInt.Index, p + mInt.Index + mInt.Length - 1, upnum, numut);

                    if (mdec.Success == false)
                    {
                        string unit = null;
                        if (isMoney)
                            unit = monut[0].ToString() + "整";
                        return integer + unit;
                    }
                    else
                    {
                        if (isMoney)
                        {
                            string jiao = upnum[p[mdec.Index] - '0'].ToString();
                            string fen = mdec.Length == 1 ? "0" : upnum[p[mdec.Index + 1] - '0'].ToString();

                            if (jiao != "0")
                            {
                                jiao += monut[1];
                            }

                            if (fen != "0")
                            {
                                jiao += fen + monut[2];
                            }

                            return integer + monut[0].ToString() + jiao;
                        }
                        else
                        {
                            return integer + ParseDecimal(p + mdec.Index, p + mdec.Index + mdec.Length - 1, upnum);
                        }
                    }
                }
            }

 

 

        }

        //操作小数部分
        static unsafe string ParseDecimal(char* p, char* end, char* upnum)
        {
            StringBuilder sb = new StringBuilder((int)(end - p));
            sb.Append(upnum[10]);
            while (p


    
 
 

您可能感兴趣的文章:

 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐




  • 特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3