纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
4.7 KiB

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