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('零');

}

}

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

感情恋爱合集

职业发展故事

常用代码片段

程序开发教程

自我备考经验

相关推荐
lightqjx8 分钟前
【数据结构】顺序表(sequential list)
c语言·开发语言·数据结构·算法
巨人张17 分钟前
信息素养Python编程题
开发语言·python
东阳马生架构19 分钟前
订单初版—5.售后退货链路中的技术问题说明文档
java
小小寂寞的城24 分钟前
JAVA策略模式demo【设计模式系列】
java·设计模式·策略模式
阿猿收手吧!41 分钟前
【计算机网络】HTTP1.0 HTTP1.1 HTTP2.0 QUIC HTTP3 究极总结
开发语言·计算机网络
JAVA学习通42 分钟前
图书管理系统(完结版)
java·开发语言
abigalexy1 小时前
深入Java锁机制
java
paishishaba1 小时前
处理Web请求路径参数
java·开发语言·后端
七七七七071 小时前
C++类对象多态底层原理及扩展问题
开发语言·c++