ValueTypeEx.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. namespace YLShipBuildLandMap.Services.Common
  7. {
  8. public static class ValueTypeEx
  9. {
  10. /// <summary>
  11. /// 返回金额的位数
  12. /// </summary>
  13. /// <param name="value">金额</param>
  14. /// <param name="index">0分位,1角位,2个位,3十位......</param>
  15. /// <returns></returns>
  16. public static string GetAmountString(this decimal value, int index)
  17. {
  18. string vchar = "";
  19. var amountStr = value.ToString("##.00").Replace(".", "").ToCharArray().Reverse().ToArray();
  20. vchar = amountStr.Length > index ? amountStr[index].ToString() : "";
  21. return vchar;
  22. }
  23. #region 中文大写
  24. /// <summary>
  25. /// 返回中文数字 ,如壹佰元整
  26. /// </summary>
  27. /// <param name="valIn"></param>
  28. /// <param name="strType">0返回金额写法,1返回数量写法</param>
  29. /// <returns></returns>
  30. public static string GetChineseNum(this decimal val, int strType = 0)
  31. {
  32. decimal valIn = Math.Abs(val);
  33. string m_1, m_2, m_3, m_4, m_5, m_6, m_7, m_8, m_9;
  34. string numNum = "0123456789.";
  35. string numChina = "零壹贰叁肆伍陆柒捌玖点";
  36. string numChinaWeigh = "个拾佰仟万拾佰仟亿拾佰仟万";
  37. //m_1.Format("%.2f", atof(m_1));
  38. m_1 = valIn.ToString("f2");
  39. m_2 = m_1;
  40. m_3 = m_4 = "";
  41. //m_2:1234-> 壹贰叁肆
  42. for (int i = 0; i < 11; i++)
  43. {
  44. //m_2=m_2.Replace(numNum.Mid(i, 1), numChina.Mid(i * 2, 2));
  45. m_2 = m_2.Replace(numNum.Substring(i, 1), numChina.Substring(i, 1));
  46. }
  47. //m_3:佰拾万仟佰拾个
  48. int iLen = m_1.Length;
  49. if (iLen > 16)
  50. {
  51. m_8 = m_9 = "越界错!";
  52. throw new Exception("数字太大,超出处理范围");
  53. }
  54. if (m_1.IndexOf('.') > 0)
  55. iLen = m_1.IndexOf('.');
  56. for (int j = iLen; j >= 1; j--)
  57. {
  58. m_3 += numChinaWeigh.Substring(j - 1, 1);
  59. }
  60. //m_4:2行+3行
  61. for (int i = 0; i < m_3.Length; i++)
  62. {
  63. m_4 += m_2.Substring(i, 1) + m_3.Substring(i, 1);
  64. }
  65. //m_5:4行去"0"后拾佰仟
  66. m_5 = m_4;
  67. m_5 = m_5.Replace("零拾", "零");
  68. m_5 = m_5.Replace("零佰", "零");
  69. m_5 = m_5.Replace("零仟", "零");
  70. //m_6:00-> 0,000-> 0
  71. m_6 = m_5;
  72. for (int i = 0; i < iLen; i++)
  73. m_6 = m_6.Replace("零零", "零");
  74. //m_7:6行去亿,万,个位"0"
  75. m_7 = m_6;
  76. m_7 = m_7.Replace("亿零万零", "亿零");
  77. m_7 = m_7.Replace("亿零万", "亿零");
  78. m_7 = m_7.Replace("零亿", "亿");
  79. m_7 = m_7.Replace("零万", "万");
  80. if (m_7.Length > 2)
  81. m_7 = m_7.Replace("零个", "个");
  82. //m_8:7行+2行小数-> 数目
  83. m_8 = m_7;
  84. m_8 = m_8.Replace("个", "");
  85. if (m_2.Substring(m_2.Length - 3, 3) != "点零零")
  86. m_8 += m_2.Substring(m_2.Length - 3, 3);
  87. //m_9:7行+2行小数-> 价格
  88. m_9 = m_7;
  89. m_9 = m_9.Replace("个", "元");
  90. if (m_2.Substring(m_2.Length - 3, 3) != "点零零")
  91. {
  92. m_9 += m_2.Substring(m_2.Length - 2, 2);
  93. m_9 = m_9.Insert(m_9.Length - 1, "角");
  94. m_9 += "分";
  95. }
  96. else m_9 += "整";
  97. if (m_9 != "零元整")
  98. m_9 = m_9.Replace("零元", "");
  99. m_9 = m_9.Replace("零分", "整");
  100. var prefix = (val < 0) ? "负" : "";
  101. if (strType == 1) //数量
  102. return prefix + m_8;
  103. else
  104. return prefix + m_9;
  105. }
  106. #endregion
  107. /// <summary>
  108. /// 按0.5取整
  109. /// </summary>
  110. /// <param name="day"></param>
  111. /// <returns></returns>
  112. public static decimal GetWorkHours(this decimal value)
  113. {
  114. var r = value % (decimal)0.5;
  115. return (r == 0 ? value : (value + Math.Abs((decimal)0.5 - r)));
  116. }
  117. public static string ReplaceHtml(this string value)
  118. {
  119. if (!string.IsNullOrEmpty(value))
  120. {
  121. value = Regex.Replace(value, @"<script[\s\S]*?</script>", "", RegexOptions.IgnoreCase);
  122. value = Regex.Replace(value, @"<noscript[\s\S]*?</noscript>", "", RegexOptions.IgnoreCase);
  123. value = Regex.Replace(value, @"<style[\s\S]*?</style>", "", RegexOptions.IgnoreCase);
  124. value = Regex.Replace(value, @"<.*?>", "", RegexOptions.IgnoreCase);
  125. value = Regex.Replace(value, @"<(.[^>]*)>", " ", RegexOptions.IgnoreCase);
  126. value = Regex.Replace(value, @"([\r\n])[\s]+", " ", RegexOptions.IgnoreCase);
  127. value = Regex.Replace(value, @"-->", " ", RegexOptions.IgnoreCase);
  128. value = Regex.Replace(value, @"<!--.*", " ", RegexOptions.IgnoreCase);
  129. value = Regex.Replace(value, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
  130. value = Regex.Replace(value, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
  131. value = Regex.Replace(value, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
  132. value = Regex.Replace(value, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
  133. value = Regex.Replace(value, @"&(nbsp|#160);", "", RegexOptions.IgnoreCase);
  134. value = Regex.Replace(value, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
  135. value = Regex.Replace(value, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
  136. value = Regex.Replace(value, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
  137. value = Regex.Replace(value, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
  138. value = Regex.Replace(value, @"&#(\d+);", " ", RegexOptions.IgnoreCase);
  139. }
  140. return value;
  141. //return !string.IsNullOrEmpty(value) ? value.Replace("<div>", "").Replace("</div>", "") : "";
  142. }
  143. }
  144. }