• NumberToChineseConverter.cs


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Data;
    
    namespace System.Windows.Controls
    {
        [ValueConversion(typeof(string), typeof(string))]
        public class NumberToChineseConverter : IValueConverter
        {
    
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                string valueString = value.ToString();
    
                decimal num = 0;
    
                if (!decimal.TryParse(valueString, out num))
                {
                    return DependencyProperty.UnsetValue;
                }
    
                string[] tmp_Array = num.ToString().Split(new char[] { '.' }, StringSplitOptions.None);
    
                // 这个是小数点前的数字
                string beforeDecimalPoint = tmp_Array[0];
                // 这个是小数点后的数字
                string afterDecimalPoint = tmp_Array.Length > 1 ? tmp_Array[1] : string.Empty;
    
                // 先将数字四位为一组分别放到一个List中
                List<ChineseNum> beforeDecimalPointGroup = new List<ChineseNum>();
                int maxIndex = beforeDecimalPoint.Length - 1;
                int count = 0;
                string tmpValue = string.Empty;
    
                for (var n = maxIndex; n >= 0; n--)
                {
                    tmpValue = string.Format("{0}{1}", beforeDecimalPoint[n].ToString(), tmpValue);
    
                    if (count == 3 || n == 0)
                    {
                        var number = new ChineseNum()
                        {
                            UnitIndex = beforeDecimalPointGroup.Count,
                            Number = tmpValue
                        };
    
                        beforeDecimalPointGroup.Add(number);
    
                        count = 0;
                        tmpValue = string.Empty;
                    }
                    else
                    {
                        count++;
                    }
                }
    
                string result = string.Empty;
                var tmp = beforeDecimalPointGroup.OrderBy(c => c.UnitIndex);
    
                // 将每4位的数字组合起来
                foreach (var a in tmp)
                {
                    result = string.Format("{1}{0}", result, a.Value);
                }
    
                // 整合小数部分
                if (!string.IsNullOrEmpty(afterDecimalPoint))
                {
                    // 增加点
                    result = string.Format("{0}{1}", result, "");
    
                    // 整合每个位置上得小数
                    for (var n = 0; n < afterDecimalPoint.Length; n++)
                    {
                        result = string.Format("{0}{1}", result, GetNumber(int.Parse(afterDecimalPoint[n].ToString())));
                    }
                }
    
                return result;
            }
    
            static string GetNumber(int n)
            {
                string num = string.Empty;
    
                switch (n)
                {
                    case 0: num = ""; break;
                    case 1: num = ""; break;
                    case 2: num = ""; break;
                    case 3: num = ""; break;
                    case 4: num = ""; break;
                    case 5: num = ""; break;
                    case 6: num = ""; break;
                    case 7: num = ""; break;
                    case 8: num = ""; break;
                    case 9: num = ""; break;
                    default: break;
                }
    
                return num;
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }
    
        public class ChineseNum
        {
            public int UnitIndex { get; set; }
    
            public string Number { get; set; }
    
            public string Value
            {
                get
                {
                    // 当只有一个0时,返回0,此例仅适用于数字0
                    if (Number == "0")
                    {
                        return PlaceHolder;
                    }
    
                    // 当只具备0时,该四位数字不需要显示
                    if (string.IsNullOrEmpty(Number.Trim(new char[] { '0' })))
                    {
                        return string.Empty;
                    }
                    else // 当具备其他数字时,组合中文大写数字
                    {
                        string value = string.Empty;
    
                        for (var n = 0; n < Number.Length; n++)
                        {
                            value = string.Format("{0}{1}{2}", value, GetNumber(n), GetUnit(n));
                        }
    
                        // 组合当前4位数所在的单位
                        return string.Format("{0}{1}", value, UnitValue(UnitIndex));
                    }
                }
            }
    
            public string PlaceHolder
            {
                get
                {
                    return "";
                }
            }
    
            string GetNumber(int index)
            {
                var n = int.Parse(Number[index].ToString());
                string num = string.Empty;
    
                // 当0为最后一位时 或者当下一位也为0时 不显示该位
                if (n == 0 && ((Number.Length == index + 1) || Number[index + 1] == '0'))
                {
                    return num;
                }
    
                switch (n)
                {
                    case 0: num = ""; break;
                    case 1: num = ""; break;
                    case 2: num = ""; break;
                    case 3: num = ""; break;
                    case 4: num = ""; break;
                    case 5: num = ""; break;
                    case 6: num = ""; break;
                    case 7: num = ""; break;
                    case 8: num = ""; break;
                    case 9: num = ""; break;
                    default: break;
                }
    
                return num;
            }
    
            /// <summary>
            /// 获取当前位置上得数字的中文大写单位
            /// </summary>
            /// <param name="index"></param>
            /// <returns></returns>
            string GetUnit(int index)
            {
                string unit = string.Empty;
    
                int unitIndex = Number.Length - index;
    
                if (Number[index] == '0')
                    return unit;
    
                switch (unitIndex)
                {
                    case 4: unit = ""; break;
                    case 3: unit = ""; break;
                    case 2: unit = ""; break;
                }
    
                return unit;
            }
    
            string UnitValue(int index)
            {
                string unit = string.Empty;
    
                switch (index)
                {
                    case 1: unit = ""; break;
                    case 2: unit = "亿"; break;
                    case 3: unit = ""; break;
                    case 4: unit = ""; break;
                    case 5: unit = ""; break;
                    case 6: unit = ""; break;
                    default: break;
                }
    
                return unit;
            }
        }
    }

    Number: 12345

    Chinese: 壹万贰千叁百肆拾伍

  • 相关阅读:
    从零开始的ESP8266探索(1)-使用Server功能搭建Web Server
    模型收集
    3D打印社区
    [教程]教你如何制作彩色的3D打印Groot
    1-5 软件安装
    1-4 打印机测试
    1-3 打印机几个重要操作
    1-1 打印机基本参数
    1-2 打印机使用注意事项
    (二 -3-3) 天猫精灵接入Home Assistant-自动发现Mqtt设备-自动生成配置信息
  • 原文地址:https://www.cnblogs.com/zisezhixin/p/4263529.html
Copyright © 2020-2023  润新知