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(ChineseNumbersfractionalPart + "分");

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

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

}

else

{

if (fractionalPart % 10 > 0)

builder.Append(ChineseNumbersfractionalPart % 10 + "分");

if (fractionalPart / 10 > 0)

builder.Insert(0, ChineseNumbersfractionalPart / 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 = ChineseNumbersnum + Unitsj + temp;

zeroFlag = true;

}

else if (zeroFlag)

{

temp = "零" + temp;

zeroFlag = false;

}

unitGroup /= 10;

}

temp += BigUnitsi;

}

else if (zeroFlag)

{

temp = "零" + temp;

zeroFlag = false;

}

builder.Insert(0, temp);

integralPart /= 10000;

}

}

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

}

}

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

感情恋爱合集

职业发展故事

常用代码片段

程序开发教程

自我备考经验

相关推荐
Thinker QAQ1 分钟前
并发编程(七):volatile——从语言规则到 CPU
java·python·go·并发编程·volatile·memory model
Fluxproxy2 分钟前
Python BeautifulSoup4 超全实战教程|解决网页乱码、标签匹配、层级查找、数据提取难题
开发语言·python
蜗牛互联网22 分钟前
从GitHub动态工作流理解确定性编排与Agent判断边界
java·人工智能·后端·github
菜鸟~noob23326 分钟前
【太空电子战】午夜锤行动中的电磁静默区:可逆干扰的实战验证【含matlab代码】
linux·开发语言·matlab·电子战
开开心心就好39 分钟前
办公软件卸载不干净?专用工具一键清残留
java·前端·人工智能·智能手机·kafka·excel·memcache
大侠归来41 分钟前
C 与 Go 语言对比:从设计哲学到工程实践
java·c语言·golang
坤坤子吖1 小时前
Python基础语法学习:条件、循环与程序控制
开发语言·笔记·python·学习
吠品1 小时前
volatile到底在拦什么:SysTick->VAL读的哪格
java·服务器·开发语言
狼与自由1 小时前
python打包
开发语言·python
进击的雷神1 小时前
OpenClaw 太重、启动太慢?我试了试这个 Rust 重写的轻量版 AI 助手网关
开发语言·人工智能·rust·zeroclaw