123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace YLShipBuildLandMap.Services.Common
- {
- public static class ValueTypeEx
- {
- /// <summary>
- /// 返回金额的位数
- /// </summary>
- /// <param name="value">金额</param>
- /// <param name="index">0分位,1角位,2个位,3十位......</param>
- /// <returns></returns>
- 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 中文大写
- /// <summary>
- /// 返回中文数字 ,如壹佰元整
- /// </summary>
- /// <param name="valIn"></param>
- /// <param name="strType">0返回金额写法,1返回数量写法</param>
- /// <returns></returns>
- 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
- /// <summary>
- /// 按0.5取整
- /// </summary>
- /// <param name="day"></param>
- /// <returns></returns>
- 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, @"<script[\s\S]*?</script>", "", RegexOptions.IgnoreCase);
- value = Regex.Replace(value, @"<noscript[\s\S]*?</noscript>", "", RegexOptions.IgnoreCase);
- value = Regex.Replace(value, @"<style[\s\S]*?</style>", "", 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, @"<!--.*", " ", RegexOptions.IgnoreCase);
- value = Regex.Replace(value, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
- value = Regex.Replace(value, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
- value = Regex.Replace(value, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
- value = Regex.Replace(value, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
- value = Regex.Replace(value, @"&(nbsp|#160);", "", RegexOptions.IgnoreCase);
- value = Regex.Replace(value, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
- value = Regex.Replace(value, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
- value = Regex.Replace(value, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
- value = Regex.Replace(value, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
- value = Regex.Replace(value, @"&#(\d+);", " ", RegexOptions.IgnoreCase);
- }
- return value;
- //return !string.IsNullOrEmpty(value) ? value.Replace("<div>", "").Replace("</div>", "") : "";
- }
- }
- }
|