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