C#将数字自动翻译为中文,比如
数字1234 , 翻译为 一千二百三十四
数字56789 , 翻译为 五万六千七百八十九
思路:
我们可以使用递归的方式,化整为零法,把复杂的数字 分割,使其简单化。
比如数字 【12 3456 7890】,我们可以按照万为单位 ,划分三个独自区间 12亿,3456万,7890
那么 我们只用处理 四位数字即可,范围 【0~9999】,那么我们只用处理千、百、十、个。
比如 4567,我们按照 4567/1000 以及 4567%1000 【商和余数】独自处理。以及类推,直到只处理个位就结束。
新建窗体应用程序NumberToChineseDemo,默认窗体Form1重命名为FormNumberToChinese,
窗体FormNumberToChinese设计如下:
新建关键处理类NumberChineseTranslateUtil.cs,
NumberChineseTranslateUtil源代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NumberToChineseDemo
{
/// <summary>
/// 数字与中文描述转化示例
/// </summary>
public class NumberChineseTranslateUtil
{
/// <summary>
/// 中文字符描述集合
/// 十索引为10
/// 百索引为11
/// 千索引为12
/// 万索引为13
/// 亿索引为14
/// </summary>
static readonly char[] ChineseCollection = new char[] { '零', '一', '二', '三', '四', '五', '六', '七', '八', '九',
'十','百','千','万','亿' };
/// <summary>
/// 数字转中文
/// 按照【个、十、百、千、万、亿】
/// 只考虑正整数和零
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public static string NumberToChinese(int number)
{
//商和余数
int quotient = 0;//商
int remainder = 0;//余数
number = Math.Abs(number);
if (number < 10)
{
//0~9
return ChineseCollection[number].ToString();
}
if (number < 100)
{
//10~99之间
quotient = number / 10;
remainder = number % 10;//考虑到 余数为0时特殊处理
return ChineseCollection[quotient].ToString() + ChineseCollection[10] + (remainder == 0 ? "" : ChineseCollection[remainder].ToString());
}
if (number < 1000)
{
//100~999
quotient = number / 100;
remainder = number % 100;
if (remainder == 0)
{
//比如300
return ChineseCollection[quotient].ToString() + ChineseCollection[11];
}
string handleRemainder = "";
if (remainder > 0 && remainder < 10)
{
handleRemainder = ChineseCollection[0].ToString();//加上零 如 502
}
return ChineseCollection[quotient].ToString() + ChineseCollection[11] + handleRemainder + NumberToChinese(remainder);
}
if (number < 10000)
{
//1000~9999
quotient = number / 1000;
remainder = number % 1000;
if (remainder == 0)
{
//比如300
return ChineseCollection[quotient].ToString() + ChineseCollection[12];
}
string handleRemainder = "";
if (remainder > 0 && remainder < 100)
{
handleRemainder = ChineseCollection[0].ToString();//加上零 如 502
}
return ChineseCollection[quotient].ToString() + ChineseCollection[12] + handleRemainder + NumberToChinese(remainder);
}
if (number < 100000000)
{
//10000~9999 9999
quotient = number / 10000;
remainder = number % 10000;
if (remainder == 0)
{
//比如300
return NumberToChinese(quotient) + ChineseCollection[13];
}
string handleRemainder = "";
if (remainder > 0 && remainder < 1000)
{
handleRemainder = ChineseCollection[0].ToString();//加上零 如 502
}
return NumberToChinese(quotient) + ChineseCollection[13] + handleRemainder + NumberToChinese(remainder);
}
else
{
//一亿或以上
quotient = number / 100000000;
remainder = number % 100000000;
if (remainder == 0)
{
//比如300
return NumberToChinese(quotient) + ChineseCollection[14];
}
string handleRemainder = "";
if (remainder > 0 && remainder < 1000)
{
handleRemainder = ChineseCollection[0].ToString();//加上零 如 502
}
return NumberToChinese(quotient) + ChineseCollection[14] + handleRemainder + NumberToChinese(remainder);
}
}
/// <summary>
/// 数字转中文
/// 考虑负数 和 正数
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public static string NumberToChineseEx(int number)
{
if (number < 0)
{
return "负" + NumberToChinese(number);
}
return NumberToChinese(number);
}
/// <summary>
/// 中文转数字
/// 只考虑正整数的情况
/// </summary>
/// <param name="chinese"></param>
/// <returns></returns>
public static int ChineseToNumber(string chinese)
{
if (chinese == "零")
{
return 0;
}
int number = 0;
int hundredMillionIndex = chinese.IndexOf('亿');//100个百万就是一亿
int tenThousandIndex = chinese.IndexOf('万');//10个千就是一万
if (hundredMillionIndex >= 0 && tenThousandIndex >= 0 && tenThousandIndex < hundredMillionIndex)
{
//不支持万亿或以上
throw new Exception($"不支持的表达式中文表达式太大了,确保不超过21亿");
}
if (hundredMillionIndex == -1 && tenThousandIndex == -1)
{
//如果不存在亿 和 万,说明是低于 10000 即 0~9999
chinese = chinese.Replace("零", "");//移除所有的零
int thousandIndex = chinese.IndexOf('千');
int hundredIndex = chinese.IndexOf('百');
int tenIndex = chinese.IndexOf('十');
if (thousandIndex >= 0)
{
//找出千的前一个字符
int thousandNumber = Array.IndexOf(ChineseCollection, chinese[thousandIndex - 1]);
if (thousandNumber == -1)
{
throw new Exception($"非法的中文表达式,表达式中间不能有空格或存在非法字符.【{chinese}】");
}
number += thousandNumber * 1000;
}
if (hundredIndex >= 0)
{
//找出百的前一个字符
int hundredNumber = Array.IndexOf(ChineseCollection, chinese[hundredIndex - 1]);
if (hundredNumber == -1)
{
throw new Exception($"非法的中文表达式,表达式中间不能有空格或存在非法字符.【{chinese}】");
}
number += hundredNumber * 100;
}
if (tenIndex >= 0)
{
//找出十的前一个字符
int tenNumber = Array.IndexOf(ChineseCollection, chinese[tenIndex - 1]);
if (tenNumber == -1)
{
throw new Exception($"非法的中文表达式,.【{chinese}】");
}
number += tenNumber * 10;
}
//如果【个位数】最后一个数,不是千、百、十
char lastChar = chinese[chinese.Length - 1];
if (lastChar != '千' && lastChar != '百' && lastChar != '十')
{
int singleDigit = Array.IndexOf(ChineseCollection, lastChar);//个位数
if (singleDigit == -1)
{
throw new Exception($"非法的中文表达式,表达式中间不能有空格或存在非法字符.【{chinese}】");
}
number += singleDigit;
}
}
else if (hundredMillionIndex >= 0 && tenThousandIndex == -1)
{
//存在 亿,不存在 万,分两部分独自处理.每一部分都小于一万
string hundredMillionPart = chinese.Substring(0, hundredMillionIndex);
int leftNumber = ChineseToNumber(hundredMillionPart);
if (leftNumber > 21)
{
throw new Exception($"不能超过21亿,不支持的表达式.【{chinese}】");
}
number += leftNumber * 100000000;
if (hundredMillionIndex + 1 < chinese.Length)
{
string tailPart = chinese.Substring(hundredMillionIndex + 1);//余下的部分
number += ChineseToNumber(tailPart);
}
}
else if (hundredMillionIndex == -1 && tenThousandIndex >= 0)
{
//不存在 亿,存在 万
string tenThousandPart = chinese.Substring(0, tenThousandIndex);
int leftNumber = ChineseToNumber(tenThousandPart);
number += leftNumber * 10000;
if (tenThousandIndex + 1 < chinese.Length)
{
string tailPart = chinese.Substring(tenThousandIndex + 1);//余下的部分
number += ChineseToNumber(tailPart);
}
}
else
{
//如果同时存在亿 和 万.那么分成三部分
string hundredMillionPart = chinese.Substring(0, hundredMillionIndex);
int leftNumber = ChineseToNumber(hundredMillionPart);
if (leftNumber > 21)
{
throw new Exception($"不能超过21亿,不支持的表达式.【{chinese}】");
}
number += leftNumber * 100000000;//亿的部分
string tenThousandPart = chinese.Substring(hundredMillionIndex + 1, tenThousandIndex - hundredMillionIndex - 1);
int middleNumber = ChineseToNumber(tenThousandPart);
number += middleNumber * 10000;//万的部分
if (tenThousandIndex + 1 < chinese.Length)
{
string tailPart = chinese.Substring(tenThousandIndex + 1);//余下的部分
number += ChineseToNumber(tailPart);
}
}
return number;
}
/// <summary>
/// 中文转数字
/// 考虑负数 和 正数
/// </summary>
/// <param name="chinese"></param>
/// <returns></returns>
public static int ChineseToNumberEx(string chinese)
{
int multiple = 1;
if (chinese.StartsWith("负"))
{
chinese = chinese.Substring(1);
multiple = -1;
}
return multiple * ChineseToNumber(chinese);
}
}
}
窗体FormNumberToChinese主要程序如下(忽略设计器自动生成的代码):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace NumberToChineseDemo
{
public partial class FormNumberToChinese : Form
{
public FormNumberToChinese()
{
InitializeComponent();
}
private void btnNumberToChinese_Click(object sender, EventArgs e)
{
txbDestChinese.Clear();
if (txbSrcNumber.Text.Trim().Length == 0)
{
txbSrcNumber.Focus();
MessageBox.Show($"请输入数字", "出错");
return;
}
int number;
if (!int.TryParse(txbSrcNumber.Text, out number))
{
txbSrcNumber.Focus();
MessageBox.Show($"请输入整数", "出错");
return;
}
try
{
txbDestChinese.Text = NumberChineseTranslateUtil.NumberToChineseEx(number);
}
catch (Exception ex)
{
MessageBox.Show($"出现异常:【{ex.Message}】", "转化出错");
}
}
private void btnChineseToNumber_Click(object sender, EventArgs e)
{
txbDestNumber.Clear();
string chinese = txbSrcChinese.Text.Trim();
if (chinese.Length == 0)
{
txbSrcChinese.Focus();
MessageBox.Show($"请输入中文", "出错");
return;
}
try
{
txbDestNumber.Text = NumberChineseTranslateUtil.ChineseToNumberEx(chinese).ToString();
}
catch (Exception ex)
{
MessageBox.Show($"出现异常:【{ex.Message}】", "转化出错");
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ji-chu/107261.html