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

}

}

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

感情恋爱合集

职业发展故事

常用代码片段

程序开发教程

自我备考经验

相关推荐
c238564 分钟前
C/C++每日一练21
c语言·开发语言·c++
qq_401700415 分钟前
Qt Modbus协议0x00
开发语言·qt
yushikong7 分钟前
关于rust开发ch32x033f8p6的一些记录
开发语言·后端·rust
wangjialelele11 分钟前
Spring Cloud 全体系学习笔记(REST、Nacos、Feign、Gateway)
java·spring boot·笔记·学习·spring·spring cloud·gateway
吴声子夜歌20 分钟前
正则表达式——环视
开发语言·正则表达式
weixin_BYSJ198722 分钟前
springboot医疗信息管理系统---附源码17465
java·javascript·spring boot·python·django·flask·php
xqqxqxxq29 分钟前
吃透 Spring 高频注解(包含SpringMVC Spring Boot)
java·spring boot·spring
都叫我大帅哥30 分钟前
Embabel 1.0 Agent Framework 完全指南:从入门到生产级实践
java
福大大架构师每日一题37 分钟前
agno v2.8.7 发布:顾问模型、精准路线、调度能力全面升级,10项关键修复一次看懂
java·开发语言·数据库
老洋葱Mr_Onion1 小时前
【C++】CSP-J复赛dp问题(含模板题)
开发语言·c++