60 lines
1.5 KiB
C#
60 lines
1.5 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace VOL.Core.Utilities
|
||
{
|
||
public class IntConvertChinese
|
||
{
|
||
public static string IntConvertToChinese(int number)
|
||
{
|
||
if (number < 0 || number > 999999999)
|
||
{
|
||
throw new ArgumentException("输入的数字超出范围(0 到 9999)");
|
||
}
|
||
|
||
if (number == 0)
|
||
{
|
||
return "零";
|
||
}
|
||
|
||
string[] chineseDigits = { "", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
|
||
string[] unitDigits = { "", "十", "百", "千" };
|
||
|
||
string result = "";
|
||
|
||
int unitIndex = 0;
|
||
while (number > 0)
|
||
{
|
||
int digit = number % 10;
|
||
if (digit > 0)
|
||
{
|
||
result = chineseDigits[digit] + unitDigits[unitIndex] + result;
|
||
}
|
||
else
|
||
{
|
||
// 处理连续的零
|
||
if (result.Length > 0 && result[0] != '零')
|
||
{
|
||
result = "零" + result;
|
||
}
|
||
}
|
||
|
||
number /= 10;
|
||
unitIndex++;
|
||
}
|
||
|
||
// 单独处理10 - 19,否则会生成一十
|
||
if (number >= 10 && number < 20)
|
||
{
|
||
return result.Substring(1);
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
}
|
||
}
|