218 lines
7.3 KiB
C#
218 lines
7.3 KiB
C#
using System;
|
|||
|
|
using System.Collections.Generic;
|
||
|
|
using System.Linq;
|
||
|
|
using System.Text;
|
||
|
|
using System.Text.RegularExpressions;
|
||
|
|
using System.Threading.Tasks;
|
||
|
|
|
||
|
|
namespace JSMachine.WMS.Common.Helper
|
||
|
|
{
|
||
|
|
public static class MathExtensions
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// 四舍五入(适用decimal类型)
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="d">要舍入的小数</param>
|
||
|
|
/// <param name="decimals">保留的小数位(负数则不处理)</param>
|
||
|
|
/// <returns></returns>
|
||
|
|
public static decimal Round(decimal? d, int decimals = 2)
|
||
|
|
{
|
||
|
|
if (!d.HasValue)
|
||
|
|
return 0;
|
||
|
|
|
||
|
|
return decimals < 0 ? d.Value : Math.Round(d.Value, decimals, MidpointRounding.AwayFromZero);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 数学函数扩展-幂运算xⁿ
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="x">底数</param>
|
||
|
|
/// <param name="n">指数</param>
|
||
|
|
/// <param name="decimals">保留的小数位(负数则不处理)</param>
|
||
|
|
/// <returns></returns>
|
||
|
|
public static decimal Pow(decimal x, decimal n, int decimals = -1)
|
||
|
|
{
|
||
|
|
decimal result = (decimal)Math.Pow((double)x, (double)n);
|
||
|
|
return Round(result, decimals);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 数学函数扩展-安全除
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="first">被除数</param>
|
||
|
|
/// <param name="second">除数</param>
|
||
|
|
/// <param name="decimals">保留的小数位(负数则不处理)</param>
|
||
|
|
/// <returns></returns>
|
||
|
|
public static decimal Division(decimal first, decimal second, int decimals = -1)
|
||
|
|
{
|
||
|
|
decimal result = second == 0 ? 0 : (first / second);
|
||
|
|
return Round(result, decimals);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 数学函数扩展-安全除
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="first">被除数</param>
|
||
|
|
/// <param name="second">除数</param>
|
||
|
|
/// <returns></returns>
|
||
|
|
public static int Division(int first, int second)
|
||
|
|
{
|
||
|
|
int result = second == 0 ? 0 : (first / second);
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 数学函数扩展-安全取余 DY20210312
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="first">被除数</param>
|
||
|
|
/// <param name="second">除数</param>
|
||
|
|
/// <returns></returns>
|
||
|
|
public static int Remainder(int first, int second)
|
||
|
|
{
|
||
|
|
int result = second == 0 ? 0 : (first % second);
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 向下取整
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="first">被除数</param>
|
||
|
|
/// <param name="second">除数</param>
|
||
|
|
/// <returns></returns>
|
||
|
|
public static int Floor(int first, int second)
|
||
|
|
{
|
||
|
|
decimal result = second == 0 ? 0 : (first / second);
|
||
|
|
return (int)Math.Floor(result);
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 向下取整
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="first">被除数</param>
|
||
|
|
/// <param name="second">数据</param>
|
||
|
|
/// <returns></returns>
|
||
|
|
public static int Floor(decimal first, decimal second)
|
||
|
|
{
|
||
|
|
decimal result = second == 0 ? 0 : (Convert.ToDecimal(first) / Convert.ToDecimal(second));
|
||
|
|
//判断是否整除
|
||
|
|
if (!StringCheck(result.ToString()))
|
||
|
|
{
|
||
|
|
return (int)Math.Floor(result) - 1;
|
||
|
|
}
|
||
|
|
return (int)result;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 安全除(计算结果为正负无穷大时,返回0)
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="first">被除数</param>
|
||
|
|
/// <param name="second">除数</param>
|
||
|
|
/// <param name="decimals">保留的小数位(负数则不处理)</param>
|
||
|
|
/// <returns></returns>
|
||
|
|
public static double Division(double first, double second, int decimals = -1)
|
||
|
|
{
|
||
|
|
// double类型不能直接判断0,会有精度丢失
|
||
|
|
// 而当除数=0时,计算结果=正负无穷大,并不会报错
|
||
|
|
if (second == 0)
|
||
|
|
{
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
double result = first / second;
|
||
|
|
result = double.IsInfinity(result) ? 0 : result;
|
||
|
|
return decimals < 0 ? result : Math.Round(result, decimals);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 两个数据是否相等
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="first"></param>
|
||
|
|
/// <param name="second"></param>
|
||
|
|
/// <param name="decimals">保留位数</param>
|
||
|
|
/// <param name="threshold">阀值</param>
|
||
|
|
/// <returns></returns>
|
||
|
|
public static bool IsEqual(decimal first, decimal second, int decimals = 2, decimal threshold = 0.001m)
|
||
|
|
{
|
||
|
|
return Math.Abs(Math.Round(first, decimals) - Math.Round(second, decimals)) <= threshold;
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 向上取整
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="first">被除数</param>
|
||
|
|
/// <param name="second">除数</param>
|
||
|
|
/// <returns></returns>
|
||
|
|
public static int Up(int first, int second)
|
||
|
|
{
|
||
|
|
decimal result = second == 0 ? 0 : (Convert.ToDecimal(first) / Convert.ToDecimal(second));
|
||
|
|
//判断是否整除
|
||
|
|
if (result.ToString().Contains("."))
|
||
|
|
return (int)Math.Floor(result) + 1;
|
||
|
|
|
||
|
|
return (int)result;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 向上取整
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="first">被除数</param>
|
||
|
|
/// <param name="second">除数</param>
|
||
|
|
/// <returns></returns>
|
||
|
|
public static int Up(double first, int second)
|
||
|
|
{
|
||
|
|
decimal result = second == 0 ? 0 : (Convert.ToDecimal(first) / Convert.ToDecimal(second));
|
||
|
|
//判断是否整除
|
||
|
|
if (result.ToString().Contains("."))
|
||
|
|
return (int)Math.Floor(result) + 1;
|
||
|
|
|
||
|
|
return (int)result;
|
||
|
|
}
|
||
|
|
/// <summary>
|
||
|
|
/// 向上取整
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="first">被除数</param>
|
||
|
|
/// <param name="second">数据</param>
|
||
|
|
/// <returns></returns>
|
||
|
|
public static int Up(decimal first, decimal second)
|
||
|
|
{
|
||
|
|
decimal result = second == 0 ? 0 : (Convert.ToDecimal(first) / Convert.ToDecimal(second));
|
||
|
|
//判断是否整除
|
||
|
|
if (!StringCheck(result.ToString()))
|
||
|
|
{
|
||
|
|
return (int)Math.Floor(result) + 1;
|
||
|
|
}
|
||
|
|
return (int)result;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
///判断最终结果,.Contains(".")作为判断依据是不对的,15.0000就会变成16,是错的
|
||
|
|
/// </summary>
|
||
|
|
/// <param name="result"></param>
|
||
|
|
/// <returns></returns>
|
||
|
|
public static bool StringCheck(string result)
|
||
|
|
{
|
||
|
|
string[] sArray = result.Split(".");
|
||
|
|
if (sArray.Length >= 2)
|
||
|
|
{
|
||
|
|
Regex r;
|
||
|
|
string pattern;
|
||
|
|
pattern = @"1|2|3|4|5|6|7|8|9";
|
||
|
|
r = new Regex(pattern);
|
||
|
|
if (r.IsMatch(sArray[1]))
|
||
|
|
{
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|