YD_SmartSports.Api/VOL.Core/Utilities/IntConvertChinese.cs
2025-06-06 16:00:39 +08:00

60 lines
1.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}
}