using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace YLShipBuildLandMap.Services.Common { public static class ValueTypeEx { /// /// 返回金额的位数 /// /// 金额 /// 0分位,1角位,2个位,3十位...... /// public static string GetAmountString(this decimal value, int index) { string vchar = ""; var amountStr = value.ToString("##.00").Replace(".", "").ToCharArray().Reverse().ToArray(); vchar = amountStr.Length > index ? amountStr[index].ToString() : ""; return vchar; } #region 中文大写 /// /// 返回中文数字 ,如壹佰元整 /// /// /// 0返回金额写法,1返回数量写法 /// public static string GetChineseNum(this decimal val, int strType = 0) { decimal valIn = Math.Abs(val); string m_1, m_2, m_3, m_4, m_5, m_6, m_7, m_8, m_9; string numNum = "0123456789."; string numChina = "零壹贰叁肆伍陆柒捌玖点"; string numChinaWeigh = "个拾佰仟万拾佰仟亿拾佰仟万"; //m_1.Format("%.2f", atof(m_1)); m_1 = valIn.ToString("f2"); m_2 = m_1; m_3 = m_4 = ""; //m_2:1234-> 壹贰叁肆 for (int i = 0; i < 11; i++) { //m_2=m_2.Replace(numNum.Mid(i, 1), numChina.Mid(i * 2, 2)); m_2 = m_2.Replace(numNum.Substring(i, 1), numChina.Substring(i, 1)); } //m_3:佰拾万仟佰拾个 int iLen = m_1.Length; if (iLen > 16) { m_8 = m_9 = "越界错!"; throw new Exception("数字太大,超出处理范围"); } if (m_1.IndexOf('.') > 0) iLen = m_1.IndexOf('.'); for (int j = iLen; j >= 1; j--) { m_3 += numChinaWeigh.Substring(j - 1, 1); } //m_4:2行+3行 for (int i = 0; i < m_3.Length; i++) { m_4 += m_2.Substring(i, 1) + m_3.Substring(i, 1); } //m_5:4行去"0"后拾佰仟 m_5 = m_4; m_5 = m_5.Replace("零拾", "零"); m_5 = m_5.Replace("零佰", "零"); m_5 = m_5.Replace("零仟", "零"); //m_6:00-> 0,000-> 0 m_6 = m_5; for (int i = 0; i < iLen; i++) m_6 = m_6.Replace("零零", "零"); //m_7:6行去亿,万,个位"0" m_7 = m_6; m_7 = m_7.Replace("亿零万零", "亿零"); m_7 = m_7.Replace("亿零万", "亿零"); m_7 = m_7.Replace("零亿", "亿"); m_7 = m_7.Replace("零万", "万"); if (m_7.Length > 2) m_7 = m_7.Replace("零个", "个"); //m_8:7行+2行小数-> 数目 m_8 = m_7; m_8 = m_8.Replace("个", ""); if (m_2.Substring(m_2.Length - 3, 3) != "点零零") m_8 += m_2.Substring(m_2.Length - 3, 3); //m_9:7行+2行小数-> 价格 m_9 = m_7; m_9 = m_9.Replace("个", "元"); if (m_2.Substring(m_2.Length - 3, 3) != "点零零") { m_9 += m_2.Substring(m_2.Length - 2, 2); m_9 = m_9.Insert(m_9.Length - 1, "角"); m_9 += "分"; } else m_9 += "整"; if (m_9 != "零元整") m_9 = m_9.Replace("零元", ""); m_9 = m_9.Replace("零分", "整"); var prefix = (val < 0) ? "负" : ""; if (strType == 1) //数量 return prefix + m_8; else return prefix + m_9; } #endregion /// /// 按0.5取整 /// /// /// public static decimal GetWorkHours(this decimal value) { var r = value % (decimal)0.5; return (r == 0 ? value : (value + Math.Abs((decimal)0.5 - r))); } public static string ReplaceHtml(this string value) { if (!string.IsNullOrEmpty(value)) { value = Regex.Replace(value, @"", "", RegexOptions.IgnoreCase); value = Regex.Replace(value, @"", "", RegexOptions.IgnoreCase); value = Regex.Replace(value, @"", "", RegexOptions.IgnoreCase); value = Regex.Replace(value, @"<.*?>", "", RegexOptions.IgnoreCase); value = Regex.Replace(value, @"<(.[^>]*)>", " ", RegexOptions.IgnoreCase); value = Regex.Replace(value, @"([\r\n])[\s]+", " ", RegexOptions.IgnoreCase); value = Regex.Replace(value, @"-->", " ", RegexOptions.IgnoreCase); value = Regex.Replace(value, @"