MathEx.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. namespace Bowin.Common.Utility
  7. {
  8. public static class MathEx
  9. {
  10. /// <summary>
  11. /// 取输入值中大于等于0的部分(取正数)
  12. /// </summary>
  13. /// <param name="input"></param>
  14. /// <returns></returns>
  15. public static int UponZero(this int input)
  16. {
  17. return (input >= 0) ? input : 0;
  18. }
  19. /// <summary>
  20. /// 取输入值中大于等于0的部分(取正数)
  21. /// </summary>
  22. /// <param name="input"></param>
  23. /// <returns></returns>
  24. public static int? UponZero(this int? input)
  25. {
  26. return ((input ?? 0) >= 0) ? input : 0;
  27. }
  28. /// <summary>
  29. /// 取输入值中大于等于0的部分(取正数)
  30. /// </summary>
  31. /// <param name="input"></param>
  32. /// <returns></returns>
  33. public static double UponZero(this double input)
  34. {
  35. return (input >= 0) ? input : 0;
  36. }
  37. /// <summary>
  38. /// 取输入值中大于等于0的部分(取正数)
  39. /// </summary>
  40. /// <param name="input"></param>
  41. /// <returns></returns>
  42. public static double? UponZero(this double? input)
  43. {
  44. return ((input ?? 0) >= 0) ? input : 0;
  45. }
  46. /// <summary>
  47. /// 取输入值中大于等于0的部分(取正数)
  48. /// </summary>
  49. /// <param name="input"></param>
  50. /// <returns></returns>
  51. public static decimal UponZero(this decimal input)
  52. {
  53. return (input >= 0) ? input : 0;
  54. }
  55. /// <summary>
  56. /// 取输入值中大于等于0的部分(取正数)
  57. /// </summary>
  58. /// <param name="input"></param>
  59. /// <returns></returns>
  60. public static decimal? UponZero(this decimal? input)
  61. {
  62. return ((input ?? 0) >= 0) ? input : 0;
  63. }
  64. /// <summary>
  65. /// 数字转大写
  66. /// </summary>
  67. /// <param name="x"></param>
  68. /// <returns></returns>
  69. public static string ConvertToChinese(this double x)
  70. {
  71. string s = x.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");
  72. string d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}");
  73. return Regex.Replace(d,
  74. ".",
  75. delegate(Match m)
  76. {
  77. return "负空空零一二三四五六七八九空空空空空空空空空十百千万亿兆京垓秭穰"[m.Value[0] - '-'].ToString();
  78. });
  79. }
  80. /// <summary>
  81. /// 数字转大写
  82. /// </summary>
  83. /// <param name="x"></param>
  84. /// <returns></returns>
  85. public static string ConvertToChinese(this int x)
  86. {
  87. string s = x.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#");
  88. string d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}");
  89. return Regex.Replace(d,
  90. ".",
  91. delegate(Match m)
  92. {
  93. return "负空空零一二三四五六七八九空空空空空空空空空十百千万亿兆京垓秭穰"[m.Value[0] - '-'].ToString();
  94. }).Replace("一十", "十");
  95. }
  96. /// <summary>
  97. /// 数字转大写金额
  98. /// </summary>
  99. /// <param name="x"></param>
  100. /// <returns></returns>
  101. public static string ConvertToChineseCurrency(double x)
  102. {
  103. string s = x.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");
  104. string d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}");
  105. return Regex.Replace(d,
  106. ".",
  107. delegate(Match m)
  108. {
  109. return "负元空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟万亿兆京垓秭穰"[m.Value[0] - '-'].ToString();
  110. });
  111. }
  112. /// <summary>
  113. /// 数字转大写金额
  114. /// </summary>
  115. /// <param name="x"></param>
  116. /// <returns></returns>
  117. public static string ConvertToChineseBig(int x)
  118. {
  119. if (x > 10 & x < 0)
  120. {
  121. throw new Exception("请输入个位数。");
  122. }
  123. string[] s = new string[] { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
  124. return s[x];
  125. }
  126. }
  127. }