c# - 汉字数字,阿拉伯数字,相互转换

本文介绍如何在C#中实现汉字数字与阿拉伯数字之间的转换。内容涵盖转换的算法和示例代码,帮助开发者在处理数字字符串时进行有效转换。
摘要由CSDN通过智能技术生成
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DesignPatterns.ChineseNumConvertor
{
    public class Node
    {
        public string Unit { get; set; }
        public string Num { get; set; }
        public Node Left { get; set; }
        public Node Right { get; set; }

        public long CalcVal()
        {
            return ((Left != null ? Left.CalcVal() : 0) + CNConvertor.numMapping[this.Num]) * CNConvertor.unitMapping[this.Unit] +
                (this.Right != null ? this.Right.CalcVal() : 0);
        }
    }

    public class CNConvertor
    {
        public static string[] units = new string[] { "亿", "万", "千", "百", "十" };
        public static string[] numbers = new string[] { "一", "二", "三", "四", "五", "六", "七", "八", "九" };
        public static Dictionary<string, int> unitMapping = new Dictionary<string, int>()
        {
             { "亿", 100000000 },
             { "万", 10000 },
             { "千", 1000 },
             { "百", 100 },
             { "十", 10 },
             { "一", 1 }
        };
        public static Dictionary<string, int> numMapping = new Dictionary<string, int>()
        {
             { "一", 1 },
             { "二", 2 },
             { "三", 3 },
             { "四", 4 },
             { "五", 5 },
             { "六", 6 },
             { "七", 7 },
             { "八", 8 },
             { "九", 9 },
             { "零", 0 }
        };

        public static Dictionary<int, string> numMappi
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值