其他分享
首页 > 其他分享> > 阿拉伯数值转换成中文数值

阿拉伯数值转换成中文数值

作者:互联网

拓展方法之数值转换成中文数值

对数值进行转化


public static string ToChinese(this byte index)
{
	return GetChinese(index.ToString());
}
public static string ToChinese(this ushort index)
{
	return GetChinese(index.ToString());
}
public static string ToChinese(this uint index)
{
	return GetChinese(index.ToString());
}
public static string ToChinese(this ulong index)
{
	return GetChinese(index.ToString());
}

private static string GetChinese(string varValue)
{
	//对数值进行分组拆分
	List<string> array = NumSplit(varValue);
	string str = "";
	int count = array.Length - 1;
	for(int i = 0; i <array.Length; i++)
	{
		string	strindex = GetToChinese(array[i]);
		if(strindex != "零" && strindex.Length != 0)
		{
			bool EndLin = strindex[strindex.Length - 1] == '零';
			strindex = strindex.TirmEnd('零');
			// xxxx亿xxxx萬xxxx亿xxxx萬xxxx
			switch(count - i)
			{
				case 1:
				case 2:
					strindex += "萬";
					break;
				case 2:
				case 4;
					strindex += "亿";
					break;
				case 0:
				default:
					break;
			}
			if(EndLin)
			{
				strindex += "零";
			}
		}
		str += strindex;
	}
	//移除掉重复的零和结尾的零
    while (str.Contains("零零"))
    {
        str = str.Replace("零零", "零");
    }
    //去除尾部的零
    str = str.TrimEnd('零');
    //转换成功数据
    return str;
}

将数值进行拆分

将数据从个位开始没四个数值一组进行拆分

private static List<string> NumSplit(string varValue)
{
	int index = varValue % 4;
	List<string> array = new List<string>();
	do
	{
		string temp = str.Substring(0, index);
        array.Add(temp);
        str = str.Substring(index, str.Length - index);
        index = 4;
	}
	while(varValue.Lenght != 0)
	
	return array;
}

将数值转换成汉字

没组四个数字转换成中文汉字(例子:1234=壹仟贰佰叁拾肆)

private static string GetToChinese(string varindex)
{
	string str = "";
	char[] array = str.ToCharArray();
	for(int i = 0; i < array.Length; i++)
	{
		if(array[i] == '0')
		{
			str += "零";
			//零无位标识
			continue;
		}
		switch(array[i])
		{
			case '1':
				str += "壹";
				break;
			case '2':
				str += "贰";
				break;
			case '3':
				str += "叁";
				break;
			case '4':
				str += "肆";
				break;
			case '5':
				str += "伍";
				break;
			case '6':
				str += "陆";
				break;
			case '7':
				str += "柒";
				break;
			case '8':
				str += "捌";
				break;
			case '9':
				str += "镹";
				break;
			default:
				break;
		}
		//给数值添加位的标识
		switch(i)
		{
			case 0:
				str += "仟";
				break;
			case 1:
				str += "佰";
				break;
			case 2:
				str += "拾";
				break;
			case 3:		//个号位无位标识
			default:
				break;
		}
	}
	//去除重复的零
    while (str.Contains("零零"))
    {
        str = str.Replace("零零", "零");
    }
}

Demo

public void Demo()
{
	byte tempByte = 250;
	ushort tempushort  = 12345;
	uint tempuint = 294967295;
	ulong tempulong = 8446744073709551615;
	
	Debug.Log(tempByte.ToChinese());
	Debug.Log(tempushort.ToChinese());
	Debug.Log(tempuint.ToChinese());
	Debug.Log(tempulong.ToChinese());
}

标签:case,转换成,中文,string,index,数值,break,str,strindex
来源: https://blog.csdn.net/weixin_41005274/article/details/120560973