SafeMath.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
  2. //
  3. // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
  4. //
  5. // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
  6. using System.Globalization;
  7. namespace Admin.NET.Core;
  8. using System;
  9. /// <summary>
  10. /// 安全的基本数学运算方法类
  11. /// </summary>
  12. public static class SafeMath
  13. {
  14. /// <summary>
  15. /// 安全加法
  16. /// </summary>
  17. /// <param name="left">左操作数</param>
  18. /// <param name="right">右操作数</param>
  19. /// <param name="precision">保留小数位数</param>
  20. /// <param name="defaultValue">默认值</param>
  21. /// <param name="throwOnError">是否抛出异常</param>
  22. /// <returns></returns>
  23. public static T Add<T>(object left, object right, int precision = 2, T defaultValue = default, bool throwOnError = true) where T : struct, IComparable, IConvertible, IFormattable
  24. {
  25. return PerformOperation(left, right, (a, b) => a + b, precision, defaultValue, throwOnError);
  26. }
  27. /// <summary>
  28. /// 安全减法
  29. /// </summary>
  30. /// <param name="left">左操作数</param>
  31. /// <param name="right">右操作数</param>
  32. /// <param name="precision">保留小数位数</param>
  33. /// <param name="defaultValue">默认值</param>
  34. /// <param name="throwOnError">是否抛出异常</param>
  35. public static T Sub<T>(object left, object right, int precision = 2, T defaultValue = default, bool throwOnError = true) where T : struct, IComparable, IConvertible, IFormattable
  36. {
  37. return PerformOperation(left, right, (a, b) => a - b, precision, defaultValue, throwOnError);
  38. }
  39. /// <summary>
  40. /// 安全乘法
  41. /// </summary>
  42. /// <param name="left">左操作数</param>
  43. /// <param name="right">右操作数</param>
  44. /// <param name="precision">保留小数位数</param>
  45. /// <param name="defaultValue">默认值</param>
  46. /// <param name="throwOnError">是否抛出异常</param>
  47. public static T Mult<T>(object left, object right, int precision = 2, T defaultValue = default, bool throwOnError = true) where T : struct, IComparable, IConvertible, IFormattable
  48. {
  49. return PerformOperation(left, right, (a, b) => a * b, precision, defaultValue, throwOnError);
  50. }
  51. /// <summary>
  52. /// 安全除法
  53. /// </summary>
  54. /// <param name="left">左操作数</param>
  55. /// <param name="right">右操作数</param>
  56. /// <param name="precision">保留小数位数</param>
  57. /// <param name="defaultValue">默认值</param>
  58. /// <param name="throwOnDivideByZero">是否抛出除以零异常</param>
  59. public static T Div<T>(object left, object right, int precision = 2, T defaultValue = default, bool throwOnDivideByZero = true) where T : struct, IComparable, IConvertible, IFormattable
  60. {
  61. return PerformOperation(left, right, (a, b) =>
  62. {
  63. if (b != 0) return a / b;
  64. if (throwOnDivideByZero) throw new DivideByZeroException("除数不能为0");
  65. return SafeConvert<decimal>(defaultValue);
  66. }, precision, defaultValue, throwOnDivideByZero);
  67. }
  68. /// <summary>
  69. /// 安全类型转换
  70. /// </summary>
  71. /// <param name="value">数据源</param>
  72. /// <param name="defaultValue">默认值</param>
  73. public static T SafeConvert<T>(object value, T defaultValue = default) where T : struct, IComparable, IConvertible, IFormattable
  74. {
  75. if (value == null) return defaultValue;
  76. try
  77. {
  78. return (T)Convert.ChangeType(value, typeof(T));
  79. }
  80. catch
  81. {
  82. return defaultValue;
  83. }
  84. }
  85. /// <summary>
  86. /// 执行数学运算
  87. /// </summary>
  88. private static T PerformOperation<T>(object left, object right, Func<decimal, decimal, decimal> operation, int precision, T defaultValue, bool throwOnError) where T : struct, IComparable, IConvertible, IFormattable
  89. {
  90. try
  91. {
  92. decimal leftValue = ConvertToDecimal(left);
  93. decimal rightValue = ConvertToDecimal(right);
  94. decimal result = operation(leftValue, rightValue);
  95. return SafeConvert(Math.Round(result, precision, MidpointRounding.AwayFromZero), defaultValue);
  96. }
  97. catch
  98. {
  99. if (throwOnError) throw;
  100. return defaultValue;
  101. }
  102. }
  103. /// <summary>
  104. /// 将输入值转换为 decimal
  105. /// </summary>
  106. public static decimal ConvertToDecimal(object value)
  107. {
  108. return value switch
  109. {
  110. null => 0m,
  111. int intValue => intValue,
  112. float floatValue => (decimal)floatValue,
  113. double doubleValue => (decimal)doubleValue,
  114. decimal decimalValue => decimalValue,
  115. long longValue => longValue,
  116. short shortValue => shortValue,
  117. byte byteValue => byteValue,
  118. string stringValue when decimal.TryParse(stringValue, NumberStyles.Any, CultureInfo.InvariantCulture, out decimal parsedValue) => parsedValue, // 尝试解析字符串
  119. _ => throw new InvalidCastException($"不支持的类型: {value.GetType().Name}")
  120. };
  121. }
  122. }