static string[] ChineseNumbers = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
static string[] ChineseUnits = { "", "拾", "佰", "仟" };
static string[] ChineseGroupUnits = { "", "万", "亿", "万亿" };
public static void Main1()
{
// 示例金额
decimal amount = 123456089.12345m;
string chineseAmount = ConvertToChineseAmount(amount);
Console.WriteLine(chineseAmount);
}
static string ConvertToChineseAmount(decimal amount)
{
if (amount == 0)
{
return "零元整";
}
string result = "";
// 处理整数部分
long integerPart = (long)Math.Floor(Math.Abs(amount));
result += ConvertIntegerPartToChinese(integerPart);
// 处理小数部分
decimal decimalPart = Math.Abs(amount) - integerPart;
result += ConvertDecimalPartToChinese(decimalPart);
return result;
}
static string ConvertIntegerPartToChinese(long integerPart)
{
string result = "";
int groupIndex = 0;
while (integerPart > 0)
{
int group = (int)(integerPart % 10000);
if (group > 0)
{
result = ConvertGroupToChinese(group) + ChineseGroupUnits[groupIndex] + result;
}
integerPart /= 10000;
groupIndex++;
}
return result + "元";
}
static string ConvertGroupToChinese(int group)
{
string result = "";
int unitIndex = 0;
bool lastNonZero = false;
while (group > 0)
{
int digit = group % 10;
if (digit > 0)
{
result = ChineseNumbers[digit] + ChineseUnits[unitIndex] + result;
lastNonZero = true;
}
else
{
if (lastNonZero)
{
result = ChineseNumbers[digit] + result;
lastNonZero = false;
}
}
group /= 10;
unitIndex++;
}
return result;
}
static string ConvertDecimalPartToChinese(decimal decimalPart)
{
if (decimalPart == 0)
{
return "整";
}
string result = "";
for (int i = 1; i <= 5; i++)
{
int digit = (int)(decimalPart * 10);
if (digit > 0)
{
result += ChineseNumbers[digit] + GetDecimalUnit(i);
}
decimalPart = (decimalPart * 10) - digit;
}
return result;
}
static string GetDecimalUnit(int position)
{
switch (position)
{
case 1: return "角";
case 2: return "分";
case 3: return "厘";
case 4: return "毫";
case 5: return "丝";
default: return "";
}
}
C# 将数值金额转换为人民币大写(支持多个小数点)
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- functionconvertCurrency(money) { //汉字的数字 varcnNums =newAr...
- 在金融、电商等领域,我们经常需要将数字金额转换为大写的人民币汉字表示,例如将1234.56转换为"壹仟贰佰叁拾肆元...