NumberToUpper 숫자 변환 중국어 상세 설명

9786 단어 NumberToUpper숫자
사용 시 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*(?<point>[.])?\d*$
        readonly static Regex CheckNumber = new Regex(@"^[\s\t]*0*(?<integer>[-+]?\d*)[.]?(?<decimal>\d*[1-9])?[0]*[\s\t]*$", RegexOptions.Compiled);

        readonly static string[] UpperNumbers =
        {
            " E c",
            " "
        };
        readonly static string[] NumberUnits =
        {
            " f |",
            " "
        };
        readonly static string[] MoneyUnits =
        {
            "A ",
            " "
        };

        #endregion

        /// <summary>
        /// </summary>
        /// <param name="number"> </param>
        /// <param name="isSimplified"> , : </param>
        /// <param name="isMoney"> ( 3 , " , , "), : </param>
        /// <param name="veryBig"> ( 15 , 10 ), : </param>
        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 <= end)
            {
                sb.Append(upnum[*p - '0']);
                p++;
            }
            return sb.ToString();
        }

        // , .....
        static unsafe string ParseInteger(char* p, char* end, char* upnum, char* numut)
        {
            int length = (int)(end - p) + 1;
            StringBuilder sb = new StringBuilder(length * 3);

            if (*p == '-')
            {
                sb.Append(numut[5]);
                p++;
                length--;
                if (*p == '.')
                {
                    sb.Append(upnum[5]);
                }
            }
            else if (*p == '+')
            {
                p++;
                length--;
            }

            bool ling = false;
            bool yi = false;
            bool wan = false;

            while (p <= end)
            {
                int num = *p - '0';         // 0-9

                if (num != 0 && ling == true)//
                {
                    sb.Append(upnum[0]);
                    ling = false;           //
                }

                if (length % 8 == 1)        // " "
                {                           //
                    int n = length / 8;     // " "

                    if (num != 0 || yi == true) //
                    {
                        if (num != 0)               // 0
                        {
                            sb.Append(upnum[num]);  //
                        }
                        if (n > 0) sb.Append(numut[4], n);
                        if (ling) ling = false;     //
                        yi = false;                 //
                        if (wan) wan = false;       //
                    }
                }
                else                                //
                {
                    var uIndex = length % 4;        //
                    if (uIndex == 1)                // " "
                    {
                        if (num != 0 || wan == true)    //
                        {
                            if (num != 0)               // 0
                            {
                                sb.Append(upnum[num]);  //
                            }
                            sb.Append(numut[uIndex]);
                            if (ling) ling = false; //
                            wan = false;            //
                            if (!yi) yi = true;     //
                        }
                    }
                    else                            //
                    {
                        if (num != 0)               // 0
                        {
                            if ((uIndex == 2 && num == 1) == false) // " " " "
                            {
                                sb.Append(upnum[num]);  //
                            }
                            sb.Append(numut[uIndex]);//
                            if (!yi) yi = true;     //
                            if (!wan) wan = true;   //
                        }
                        else if (ling == false)
                        {
                            ling = true;
                        }
                    }
                }

                length--;
                p++;
            }
            return sb.ToString();
        }

    }
}


var str = NumberToUpper.Go("467412346546454.4564768");
Console.WriteLine(str);
str = NumberToUpper.Go("467412346546454.4564768", true);
Console.WriteLine();
Console.WriteLine(str);
str = NumberToUpper.Go("467412346546454.4564768", false, false);
Console.WriteLine();
Console.WriteLine(str);
str = NumberToUpper.Go("7672313576513214657465413244563203246.1235", false, false, true);
Console.WriteLine();
Console.WriteLine(str);

좋은 웹페이지 즐겨찾기