C#数字转大写人民币

见过不少人、经过不少事、也吃过不少苦,感悟世事无常、人心多变,靠着回忆将往事串珠成链,聊聊感情、谈谈发展,我慢慢写、你一点一点看......

C#实现的将数字金额转换为中文大写金额的辅助类,能处理较大数额。

public static class NumberToChineseCapitalHelper

{

private static readonly char[] ChineseNumbers = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' };

private static readonly string[] Units = { "", "拾", "佰", "仟" };

private static readonly string[] BigUnits = { "", "万", "亿", "兆" };

public static string ConvertToChineseCapital(decimal amount)

{

if (amount == 0m)

return "零元整";

if (amount >= 10000000000000000m)

throw new ArgumentOutOfRangeException(nameof(amount), "金额超出处理范围。");

StringBuilder builder = new StringBuilder();

long integralPart = (long)Math.Floor(amount);

int fractionalPart = (int)Math.Round((amount - integralPart) * 100);

if (fractionalPart < 10)

{

if (fractionalPart > 0)

builder.Append(ChineseNumbers[fractionalPart] + "分");

if (fractionalPart == 0 || integralPart > 0)

builder.Insert(0, "零角");

}

else

{

if (fractionalPart % 10 > 0)

builder.Append(ChineseNumbers[fractionalPart % 10] + "分");

if (fractionalPart / 10 > 0)

builder.Insert(0, ChineseNumbers[fractionalPart / 10] + "角");

}

if (integralPart == 0)

{

builder.Insert(0, "零元");

}

else

{

builder.Insert(0, "元");

bool zeroFlag = false;

for (int i = 0; integralPart > 0; i++)

{

string temp = "";

int unitGroup = (int)(integralPart % 10000);

if (unitGroup > 0)

{

for (int j = 0; j < 4; j++)

{

int num = unitGroup % 10;

if (num > 0)

{

temp = ChineseNumbers[num] + Units[j] + temp;

zeroFlag = true;

}

else if (zeroFlag)

{

temp = "零" + temp;

zeroFlag = false;

}

unitGroup /= 10;

}

temp += BigUnits[i];

}

else if (zeroFlag)

{

temp = "零" + temp;

zeroFlag = false;

}

builder.Insert(0, temp);

integralPart /= 10000;

}

}

return builder.ToString().TrimStart('零');

}

}

关注我,不失联。有啥问题请留言。

感情恋爱合集

职业发展故事

常用代码片段

程序开发教程

自我备考经验

相关推荐
dhxhsgrx1 小时前
PYTHON训练营DAY25
java·开发语言·python
不知几秋2 小时前
数字取证-内存取证(volatility)
java·linux·前端
敲代码的 蜡笔小新3 小时前
【行为型之中介者模式】游戏开发实战——Unity复杂系统协调与通信架构的核心秘诀
unity·设计模式·c#·中介者模式
风逸hhh3 小时前
python打卡day25@浙大疏锦行
开发语言·python
刚入门的大一新生3 小时前
C++初阶-string类的模拟实现与改进
开发语言·c++
程序猿多布4 小时前
使用Visual Studio将C#程序发布为.exe文件
c#·visual studio
chxii5 小时前
5java集合框架
java·开发语言
老衲有点帅5 小时前
C#多线程Thread
开发语言·c#
C++ 老炮儿的技术栈5 小时前
什么是函数重载?为什么 C 不支持函数重载,而 C++能支持函数重载?
c语言·开发语言·c++·qt·算法