c# 金钱大写转小写工具类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TJCFinanceWriteOff.BizLogic.Common
{
    /// <summary>
    /// C#金额大写转小写
    /// </summary>
    public static class MoneyUtil
    {

        public static string RMBChineseConvertToNumber(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return string.Empty;
            }

            str = str.Replace("", "").Replace("", "");

            var arr = str.ToCharArray();
            double money = 0;   //最终的金额

            bool yi = false;
            bool wan = false;
            bool qian = false;
            bool bai = false;
            bool shi = false;
            bool yuan = false;
            bool jiao = false;
            bool fen = false;

            for (int i = (arr.Length - 1); i >= 0; i--)
            {
                double cur = 0;

                if (arr[i] == '')
                {
                    fen = true;
                    continue;
                }
                else if (arr[i] == '')
                {
                    jiao = true;
                    fen = false;
                    continue;
                }
                else if (arr[i] == '')
                {
                    yuan = true;
                    jiao = false;
                    fen = false;
                    continue;
                }
                else if (arr[i] == '')
                {
                    shi = true;
                    yuan = false;
                    jiao = false;
                    fen = false;
                    continue;
                }
                else if (arr[i] == '')
                {
                    bai = true;
                    shi = false;
                    yuan = false;
                    jiao = false;
                    fen = false;
                    continue;
                }
                else if (arr[i] == '' || arr[i] == '')
                {
                    qian = true;
                    bai = false;
                    shi = false;
                    yuan = false;
                    jiao = false;
                    fen = false;
                    continue;
                }
                else if (arr[i] == '')
                {
                    wan = true;
                    qian = false;
                    bai = false;
                    shi = false;
                    yuan = false;
                    jiao = false;
                    fen = false;
                    continue;
                }
                else if (arr[i] == '亿')
                {
                    yi = true;
                    wan = false;
                    qian = false;
                    bai = false;
                    shi = false;
                    yuan = false;
                    jiao = false;
                    fen = false;
                    continue;
                }

                if (fen) cur = ConvertNameToSmall(arr[i]) * 0.01;
                else if (jiao)
                {
                    cur = ConvertNameToSmall(arr[i]) * 0.1;
                    jiao = false;
                }
                else if (shi) cur = ConvertNameToSmall(arr[i]) * 10;
                else if (bai) cur = ConvertNameToSmall(arr[i]) * 100;
                else if (qian) cur = ConvertNameToSmall(arr[i]) * 1000;
                else if (yuan) cur = ConvertNameToSmall(arr[i]) * 1;
                else
                {
                    cur = ConvertNameToSmall(arr[i]);
                }

                if (yi)
                {
                    cur = cur * 100000000;
                }
                else if (wan)
                {
                    cur = cur * 10000;
                }
                else
                {
                    //  cur = cur;
                }

                money += cur;
            }

            return MoneyToString(money);
        }

        private static int ConvertNameToSmall(char str)
        {
            int name = 0;
            switch (str.ToString())
            {
                case "": name = 0; break;
                case "": name = 1; break;
                case "": name = 2; break;
                case "": name = 3; break;
                case "": name = 4; break;
                case "": name = 5; break;
                case "": name = 6; break;
                case "": name = 7; break;
                case "": name = 8; break;
                case "": name = 9; break;
                default: break;
            }
            return name;
        }

        public static string MoneyToString(double money)
        {
            var moneyArray = money.ToString().ToList();
            int index = moneyArray.IndexOf('.');
            int integerLengthIndex = index != -1 ? index : moneyArray.Count;
            while (integerLengthIndex - 3 > 0)
            {
                moneyArray.Insert(integerLengthIndex - 3, ',');
                integerLengthIndex = moneyArray.IndexOf(',');
            }
            StringBuilder sb = new StringBuilder("");
            sb.Append(string.Concat(moneyArray));
            switch (moneyArray.IndexOf('.'))
            {
                case int i when i == moneyArray.Count - 2:
                    sb.Append("0");
                    break;
                case -1:
                    sb.Append(".00");
                    break;
                default:
                    break;
            }
            return sb.ToString();
        }
    }
}

 

posted @ 2019-10-29 09:54  SpringCore  阅读(247)  评论(0编辑  收藏  举报