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

}

}

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

感情恋爱合集

职业发展故事

常用代码片段

程序开发教程

自我备考经验

相关推荐
nbsaas-boot7 分钟前
Java 正则表达式白皮书:语法详解、工程实践与常用表达式库
开发语言·python·mysql
岁忧8 分钟前
(LeetCode 面试经典 150 题 ) 11. 盛最多水的容器 (贪心+双指针)
java·c++·算法·leetcode·面试·go
chao_78911 分钟前
二分查找篇——搜索旋转排序数组【LeetCode】两次二分查找
开发语言·数据结构·python·算法·leetcode
CJi0NG13 分钟前
【自用】JavaSE--算法、正则表达式、异常
java
风无雨39 分钟前
GO 启动 简单服务
开发语言·后端·golang
Hellyc39 分钟前
用户查询优惠券之缓存击穿
java·redis·缓存
斯普信专业组1 小时前
Go语言包管理完全指南:从基础到最佳实践
开发语言·后端·golang
今天又在摸鱼1 小时前
Maven
java·maven
老马啸西风1 小时前
maven 发布到中央仓库常用脚本-02
java·maven
代码的余温1 小时前
MyBatis集成Logback日志全攻略
java·tomcat·mybatis·logback