C#商品金额大小写转换
见图
代码如下
public string NumToChinese(string x) { //数字转换为中文后的数组 string[] P_array_num = new string[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" }; //为数字位数建立一个位数组 string[] P_array_digit = new string[] { "", "拾", "佰", "仟" }; //为数字单位建立一个单位数组 string[] P_array_units = new string[] { "", "万", "亿", "万亿" }; string P_str_returnValue = ""; //返回值 int finger = 0; //字符位置指针 int P_int_m = x.Length % 4; //取模 int P_int_k = 0; if (P_int_m > 0) P_int_k = x.Length / 4 + 1; else P_int_k = x.Length / 4; //外层循环,四位一组,每组最后加上单位: ",万亿,",",亿,",",万," for (int i = P_int_k; i > 0; i--) { int P_int_L = 4; if (i == P_int_k && P_int_m != 0) P_int_L = P_int_m; //得到一组四位数 string four = x.Substring(finger, P_int_L); int P_int_l = four.Length; //内层循环在该组中的每一位数上循环 for (int j = 0; j < P_int_l; j++) { //处理组中的每一位数加上所在的位 int n = Convert.ToInt32(four.Substring(j, 1)); if (n == 0) { if (j < P_int_l - 1 && Convert.ToInt32(four.Substring(j + 1, 1)) > 0 && !P_str_returnValue.EndsWith(P_array_num[n])) P_str_returnValue += P_array_num[n]; } else { if (!(n == 1 && (P_str_returnValue.EndsWith(P_array_num[0]) | P_str_returnValue.Length == 0) && j == P_int_l - 2)) P_str_returnValue += P_array_num[n]; P_str_returnValue += P_array_digit[P_int_l - j - 1]; } } finger += P_int_L; //每组最后加上一个单位:",万,",",亿," 等 if (i < P_int_k) //如果不是最高位的一组 { if (Convert.ToInt32(four) != 0) //如果所有4位不全是0则加上单位",万,",",亿,"等 P_str_returnValue += P_array_units[i - 1]; } else { //处理最高位的一组,最后必须加上单位 P_str_returnValue += P_array_units[i - 1]; } } return P_str_returnValue; }
调用
private void btn_transform_Click(object sender, EventArgs e) { int P_int_temp;//定义整型变量 if (int.TryParse(txt_lower.Text,out P_int_temp)) { txt_upper.Text = //获取转换为大写金额的字符串 new Upper().NumToChinese(txt_lower.Text); } else { MessageBox.Show(//错误提示信息 "请输入正确整数数值","提示!"); } }
付费内容,请联系本人QQ:1002453261
本文来自博客园,作者:明志德道,转载请注明原文链接:https://www.cnblogs.com/for-easy-fast/p/16462267.html
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析