72 lines
2.3 KiB
C#
Raw Permalink Normal View History

2025-06-03 13:44:45 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Pilot.Report.Exploitation.AccountsReceivable
{
public class ProgramNumber
{
public static void Main()
{
decimal number = 1234567.89m;
string chineseNumber = ToChineseNumber(number);
Console.WriteLine($"数字 {number} 的中文大写数字是:{chineseNumber}");
}
public static string ToChineseNumber(decimal number)
{
// 定义数字和单位
string[] digits = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
string[] units = { "", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿" };
// 将数字转换为整数和小数部分
string numberStr = number.ToString("N2").Replace(",", ""); // 去掉千分位分隔符
string[] parts = numberStr.Split('.');
string integerPart = parts[0];
string decimalPart = parts.Length > 1 ? parts[1] : "00";
// 处理整数部分
string result = "";
for (int i = 0; i < integerPart.Length; i++)
{
int digit = int.Parse(integerPart[integerPart.Length - i - 1].ToString());
int unitIndex = integerPart.Length - i - 1;
if (digit != 0)
{
result = digits[digit] + units[unitIndex] + result;
}
else
{
result = "零" + result;
}
}
// 处理小数部分
if (decimalPart != "00")
{
result += "点";
foreach (char c in decimalPart)
{
result += digits[int.Parse(c.ToString())];
}
}
// 去掉多余的零
result = result.Replace("零零", "零").Replace("零万", "万").Replace("零亿", "亿");
if (result.StartsWith("零"))
{
result = result.Substring(1);
}
if (result.EndsWith("零"))
{
result = result.Substring(0, result.Length - 1);
}
return result;
}
}
}