00-Extensions.Format.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. namespace LeaRun.Util.Extension
  2. {
  3. /// <summary>
  4. /// 格式化扩展
  5. /// </summary>
  6. public static partial class Extensions
  7. {
  8. /// <summary>
  9. /// 获取描述
  10. /// </summary>
  11. /// <param name="value">布尔值</param>
  12. public static string Description(this bool value)
  13. {
  14. return value ? "是" : "否";
  15. }
  16. /// <summary>
  17. /// 获取描述
  18. /// </summary>
  19. /// <param name="value">布尔值</param>
  20. public static string Description(this bool? value)
  21. {
  22. return value == null ? "" : Description(value.Value);
  23. }
  24. /// <summary>
  25. /// 获取格式化字符串
  26. /// </summary>
  27. /// <param name="number">数值</param>
  28. /// <param name="defaultValue">空值显示的默认文本</param>
  29. public static string Format(this int number, string defaultValue = "")
  30. {
  31. if (number == 0)
  32. return defaultValue;
  33. return number.ToString();
  34. }
  35. /// <summary>
  36. /// 获取格式化字符串
  37. /// </summary>
  38. /// <param name="number">数值</param>
  39. /// <param name="defaultValue">空值显示的默认文本</param>
  40. public static string Format(this int? number, string defaultValue = "")
  41. {
  42. return Format(number.SafeValue(), defaultValue);
  43. }
  44. /// <summary>
  45. /// 获取格式化字符串
  46. /// </summary>
  47. /// <param name="number">数值</param>
  48. /// <param name="defaultValue">空值显示的默认文本</param>
  49. public static string Format(this decimal number, string defaultValue = "")
  50. {
  51. if (number == 0)
  52. return defaultValue;
  53. return string.Format("{0:0.##}", number);
  54. }
  55. /// <summary>
  56. /// 获取格式化字符串
  57. /// </summary>
  58. /// <param name="number">数值</param>
  59. /// <param name="defaultValue">空值显示的默认文本</param>
  60. public static string Format(this decimal? number, string defaultValue = "")
  61. {
  62. return Format(number.SafeValue(), defaultValue);
  63. }
  64. /// <summary>
  65. /// 获取格式化字符串
  66. /// </summary>
  67. /// <param name="number">数值</param>
  68. /// <param name="defaultValue">空值显示的默认文本</param>
  69. public static string Format(this double number, string defaultValue = "")
  70. {
  71. if (number == 0)
  72. return defaultValue;
  73. return string.Format("{0:0.##}", number);
  74. }
  75. /// <summary>
  76. /// 获取格式化字符串
  77. /// </summary>
  78. /// <param name="number">数值</param>
  79. /// <param name="defaultValue">空值显示的默认文本</param>
  80. public static string Format(this double? number, string defaultValue = "")
  81. {
  82. return Format(number.SafeValue(), defaultValue);
  83. }
  84. /// <summary>
  85. /// 获取格式化字符串,带¥
  86. /// </summary>
  87. /// <param name="number">数值</param>
  88. public static string FormatRmb(this decimal number)
  89. {
  90. if (number == 0)
  91. return "¥0";
  92. return string.Format("¥{0:0.##}", number);
  93. }
  94. /// <summary>
  95. /// 获取格式化字符串,带¥
  96. /// </summary>
  97. /// <param name="number">数值</param>
  98. public static string FormatRmb(this decimal? number)
  99. {
  100. return FormatRmb(number.SafeValue());
  101. }
  102. /// <summary>
  103. /// 获取格式化字符串,带%
  104. /// </summary>
  105. /// <param name="number">数值</param>
  106. public static string FormatPercent(this decimal number)
  107. {
  108. if (number == 0)
  109. return string.Empty;
  110. return string.Format("{0:0.##}%", number);
  111. }
  112. /// <summary>
  113. /// 获取格式化字符串,带%
  114. /// </summary>
  115. /// <param name="number">数值</param>
  116. public static string FormatPercent(this decimal? number)
  117. {
  118. return FormatPercent(number.SafeValue());
  119. }
  120. /// <summary>
  121. /// 获取格式化字符串,带%
  122. /// </summary>
  123. /// <param name="number">数值</param>
  124. public static string FormatPercent(this double number)
  125. {
  126. if (number == 0)
  127. return string.Empty;
  128. return string.Format("{0:0.##}%", number);
  129. }
  130. /// <summary>
  131. /// 获取格式化字符串,带%
  132. /// </summary>
  133. /// <param name="number">数值</param>
  134. public static string FormatPercent(this double? number)
  135. {
  136. return FormatPercent(number.SafeValue());
  137. }
  138. }
  139. }