You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
186 lines
6.4 KiB
C#
186 lines
6.4 KiB
C#
// <copyright file="NumberConverter.cs" company="jindongfang">
|
|
// Copyright (c) jindongfang. All rights reserved.
|
|
// </copyright>
|
|
|
|
using System;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace Fk.Utils
|
|
{
|
|
/// <summary>
|
|
/// 阿拉伯数字转中文数字,中文数字转阿拉伯数字。
|
|
/// </summary>
|
|
public static class NumberConverter
|
|
{
|
|
/// <summary>
|
|
/// 阿拉伯数字转换成中文数字
|
|
/// </summary>
|
|
/// <param name="x">input</param>
|
|
/// <returns>output</returns>
|
|
public static string NumToChinese(long x)
|
|
{
|
|
return NumToChinese(x.ToString());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 阿拉伯数字转换成中文数字
|
|
/// </summary>
|
|
/// <param name="x">input</param>
|
|
/// <returns>output</returns>
|
|
public static string NumToChinese(string x)
|
|
{
|
|
string[] pArrayNum = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
|
|
/*为数字位数建立一个位数组*/
|
|
string[] pArrayDigit = { string.Empty, "十", "百", "千" };
|
|
/*为数字单位建立一个单位数组*/
|
|
string[] pArrayUnits = { string.Empty, "万", "亿", "万亿" };
|
|
string pStrReturnValue = string.Empty; // 返回值
|
|
int finger = 0; // 字符位置指针
|
|
int pIntM = x.Length % 4; // 取模
|
|
int pIntK;
|
|
if (pIntM > 0)
|
|
{
|
|
pIntK = (x.Length / 4) + 1;
|
|
}
|
|
else
|
|
{
|
|
pIntK = x.Length / 4;
|
|
}
|
|
/*外层循环,四位一组,每组最后加上单位: ",万亿,",",亿,",",万," */
|
|
for (int i = pIntK; i > 0; i--)
|
|
{
|
|
int pIntL = 4;
|
|
if (i == pIntK && pIntM != 0)
|
|
{
|
|
pIntL = pIntM;
|
|
}
|
|
/*得到一组四位数*/
|
|
string four = x.Substring(finger, pIntL);
|
|
int p_int_l = four.Length;
|
|
|
|
// 内层循环在该组中的每一位数上循环
|
|
for (int j = 0; j < p_int_l; j++)
|
|
{
|
|
// 处理组中的每一位数加上所在的位
|
|
int n = Convert.ToInt32(four.Substring(j, 1));
|
|
if (n == 0)
|
|
{
|
|
if (j < p_int_l - 1 && Convert.ToInt32(four.Substring(j + 1, 1)) > 0 && !pStrReturnValue.EndsWith(pArrayNum[n]))
|
|
{
|
|
pStrReturnValue += pArrayNum[n];
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (!(n == 1 && (pStrReturnValue.EndsWith(pArrayNum[0]) | pStrReturnValue.Length == 0) && j == p_int_l - 2))
|
|
{
|
|
pStrReturnValue += pArrayNum[n];
|
|
}
|
|
|
|
pStrReturnValue += pArrayDigit[p_int_l - j - 1];
|
|
}
|
|
}
|
|
finger += pIntL;
|
|
|
|
// 每组最后加上一个单位:",万,",",亿," 等
|
|
// 如果不是最高位的一组
|
|
if (i < pIntK)
|
|
{
|
|
if (Convert.ToInt32(four) != 0)
|
|
{
|
|
// 如果所有4位不全是0则加上单位",万,",",亿,"等
|
|
pStrReturnValue += pArrayUnits[i - 1];
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// 处理最高位的一组,最后必须加上单位
|
|
pStrReturnValue += pArrayUnits[i - 1];
|
|
}
|
|
}
|
|
return pStrReturnValue;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将中文数字转换阿拉伯数字
|
|
/// </summary>
|
|
/// <param name="cnum">汉字数字</param>
|
|
/// <returns>长整型阿拉伯数字</returns>
|
|
public static long ChineseToNum(string cnum)
|
|
{
|
|
cnum = Regex.Replace(cnum, "\\s+", string.Empty);
|
|
long firstUnit = 1; // 一级单位
|
|
long secondUnit = 1; // 二级单位
|
|
long result = 0; // 结果
|
|
// 从低到高位依次处理
|
|
for (int i = cnum.Length - 1; i > -1; --i)
|
|
{
|
|
long tmpUnit = CharToUnit(cnum[i]); // 临时单位变量
|
|
// 判断此位是数字还是单位
|
|
if (tmpUnit > firstUnit)
|
|
{
|
|
// 是的话就赋值,以备下次循环使用
|
|
firstUnit = tmpUnit;
|
|
secondUnit = 1;
|
|
|
|
// 处理如果是"十","十一"这样的开头的
|
|
if (i == 0)
|
|
{
|
|
result += firstUnit * secondUnit;
|
|
}
|
|
continue; // 结束本次循环
|
|
}
|
|
if (tmpUnit > secondUnit)
|
|
{
|
|
secondUnit = tmpUnit;
|
|
continue;
|
|
}
|
|
|
|
// 如果是数字,则和单位想乘然后存到结果里
|
|
result += firstUnit * secondUnit * CharToNumber(cnum[i]);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 中文数字转阿拉伯数字
|
|
/// </summary>
|
|
/// <param name="c">input</param>
|
|
/// <returns>output</returns>
|
|
private static long CharToNumber(char c)
|
|
{
|
|
switch (c)
|
|
{
|
|
case '一': return 1;
|
|
case '二': return 2;
|
|
case '三': return 3;
|
|
case '四': return 4;
|
|
case '五': return 5;
|
|
case '六': return 6;
|
|
case '七': return 7;
|
|
case '八': return 8;
|
|
case '九': return 9;
|
|
case '零': return 0;
|
|
default: return -1;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 转换单位.
|
|
/// </summary>
|
|
/// <param name="c">input</param>
|
|
/// <returns>output</returns>
|
|
private static long CharToUnit(char c)
|
|
{
|
|
switch (c)
|
|
{
|
|
case '十': return 10;
|
|
case '百': return 100;
|
|
case '千': return 1000;
|
|
case '万': return 10000;
|
|
case '亿': return 100000000;
|
|
default: return 1;
|
|
}
|
|
}
|
|
}
|
|
} |