将人民币大写金额格式转为数字金额格式C#代码
来源:互联网 发布:4.5铅弹规格尺寸数据 编辑:程序博客网 时间:2024/05/29 04:07
网上相关资料都是将数字金额转换为大写金额的代码,因为需要所以自己实现了将大写格式转为数字金额 格式,有相同需要的可以参考了
/// <summary> /// 将人民币大写金额转换为数字金额 /// </summary> public class RMBConversion { /// <summary> /// /// </summary> /// <param name="value"></param> /// <returns></returns> private static decimal ToLowerCaseFunction(string value) { List<string> list = new List<string>(); if (value.Contains("仟")) { var arr1 = Regex.Split(value, @"仟", RegexOptions.IgnoreCase); if (arr1 != null && arr1.Length > 0) { list.Add(arr1[0] + "000"); } if(arr1.Length > 1) { value = arr1[1]; if(value.Contains("佰")) { var arr2 = Regex.Split(value, @"佰", RegexOptions.IgnoreCase); if (arr2 != null && arr2.Length > 0) { list.Add(arr2[0] + "00"); } if (arr2.Length > 1) { value = arr2[1]; if (value.Contains("拾")) { var arr3 = Regex.Split(value, @"拾", RegexOptions.IgnoreCase); if (arr3 != null && arr3.Length > 0) { list.Add(arr3[0] + "0"); } if (arr3.Length > 1) { list.Add(arr3[1]); } } else //不含拾 拾 { list.Add(value); } } } else //不含佰的情况 { if(value.Contains("拾")) { var arr3 = Regex.Split(value, @"拾", RegexOptions.IgnoreCase); if (arr3 != null && arr3.Length > 0) { list.Add(arr3[0] + "0"); } if (arr3.Length > 1) { list.Add(arr3[1]); } } else //不含拾 拾 { list.Add(value); } } } } else //不含仟的情况 { if(value.Contains("佰")) { var arr2 = Regex.Split(value, @"佰", RegexOptions.IgnoreCase); if (arr2 != null && arr2.Length > 0) { list.Add(arr2[0] + "00"); } if (arr2.Length > 1) { value = arr2[1]; if (value.Contains("拾")) { var arr3 = Regex.Split(value, @"拾", RegexOptions.IgnoreCase); if (arr3 != null && arr3.Length > 0) { list.Add(arr3[0] + "0"); } if (arr3.Length > 1) { list.Add(arr3[1]); } } else //不含拾 { list.Add(value); } } } else //不含佰的情况 { if(value.Contains("拾")) { var arr3 = Regex.Split(value, @"拾", RegexOptions.IgnoreCase); if (arr3 != null && arr3.Length > 0) { list.Add(arr3[0] + "0"); } if (arr3.Length > 1) { list.Add(arr3[1]); } } else //不含拾 { list.Add(value); } } } decimal result = 0; if(list != null && list.Count > 0) { foreach (var item in list) { decimal number = 0; if (decimal.TryParse(item, out number)) { result += number; } } } return result; } /// <summary> /// 将人民币金额转为数字格式 /// </summary> /// <param name="str"></param> /// <returns></returns> public static decimal ToLowerCase(string str) { NameValueCollection filterList = new NameValueCollection(); filterList.Add("零", "0"); filterList.Add("壹", "1"); filterList.Add("贰", "2"); filterList.Add("叁", "3"); filterList.Add("肆", "4"); filterList.Add("伍", "5"); filterList.Add("五", "5"); filterList.Add("陆", "6"); filterList.Add("柒", "7"); filterList.Add("捌", "8"); filterList.Add("玖", "9"); filterList.Add("元", "圆"); filterList.Add("正", "整"); filterList.Add("千", "仟"); filterList.Add("整", ""); for (int i = 0; i < filterList.Count; i++) { string key = filterList.GetKey(i); if (str.Contains(key)) { str = str.Replace(key, filterList.Get(i)); } } List<decimal> decimalList = new List<decimal>(); decimal tempValue = 0; //拆分 if (str.Contains("亿")) { var arr = Regex.Split(str, @"亿", RegexOptions.IgnoreCase); tempValue = ToLowerCaseFunction(arr[0]); if (arr.Length > 1) { str = arr[1]; } decimalList.Add(tempValue * 100000000); } if (str.Contains("万")) { var arr = Regex.Split(str, @"万", RegexOptions.IgnoreCase); tempValue = ToLowerCaseFunction(arr[0]); if (arr.Length > 1) { str = arr[1]; } decimalList.Add(tempValue * 10000); } if (str.Contains("圆")) { var arr = Regex.Split(str, @"圆", RegexOptions.IgnoreCase); tempValue = ToLowerCaseFunction(arr[0]); if (arr.Length > 1) { str = arr[1]; } decimalList.Add(tempValue); } if (str.Contains("角") || str.Contains("分")) { str = str.Replace("角", "").Replace("分", ""); decimal.TryParse(str, out tempValue); decimalList.Add(tempValue / 100); } return Math.Round(decimalList.Sum(), 2); //保留两位小数 } }
string rmbStr = "肆拾贰万贰仟肆佰肆拾壹元叁角贰分"; //大写金额转换 decimal money = RMBConversion.ToLowerCase(rmbStr); result:422441.32
阅读全文
0 0
- 将人民币大写金额格式转为数字金额格式C#代码
- 人民币金额中数字格式转化为中文的人民币大写格式的全部代码
- 蓝桥杯 人民币金额大写 格式转换
- 数字金额转为大写金额
- 数字金额转为大写金额
- C#人民币金额大写
- C#将人民币转换成大写金额
- 将数字转为大写的金额
- Qt将数字转为中文大写金额
- 人民币数字金额转大写金额
- 数字金额转为大写金额(C#)
- 数字金额转为大写金额(C#)
- 数字金额转为大写金额(C#)
- C#转换人民币大写金额
- C#人民币金额大写转换
- C# 人民币金额转大写
- ABAP--将数字金额转换为人民币大写字符串
- ABAP--将数字金额转换为人民币大写字符串
- Android2017Google IO
- kernel通过psci的smc让从cpu boot的过程
- NOR FLASH读、写、擦原理与实现(1)——性能简述与术语解释
- USACO 2.4 Fractions to Decimals
- postgresql和postgis的安装
- 将人民币大写金额格式转为数字金额格式C#代码
- java中map、list转json
- oracle实例
- 【C#】获取桌面分辨率宽高(是否包含任务栏)
- (转)BlackRock:全球最大资管公司如何一步步倒戈人工智能?
- LeetCode 2. Add Two Numbers ***** 进位的巧妙解决
- JavaScript基础(一)- 介绍、实现、输出
- 这只兔子能够带来什么收益?
- 一些常见问题