namespace IStation.Curve
{
///
/// 辅助类
///
public static class UtilsHelper
{
///
/// 求两个数值的最小值
///
public static double Min(this double first, double second)
{
return first > second ? second : first;
}
///
/// 求两个数值的最大值
///
public static double Max(this double first, double second)
{
return first > second ? first : second;
}
///
/// 是否为中间数
///
public static bool IsMiddle(this double x, double x1, double x2)
{
return (x >= x1 && x <= x2) || (x <= x1 && x >= x2);
}
#region 16进制字符
///
/// 16进制字符转十进制:如A1就是10*16+1
///
public static int GetDecimalByHexString(string strHexStr)
{
if (strHexStr.Count() != 2)
return 0;
return GetDecimalByHexChar(strHexStr[0]) * 16 + GetDecimalByHexChar(strHexStr[1]);
}
///
/// 获取16进制字符
///
public static short GetDecimalByHexChar(char strHexStr)
{
switch (strHexStr)
{
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case 'a':
case 'A':
return 10;
case 'b':
case 'B':
return 11;
case 'c':
case 'C':
return 12;
case 'd':
case 'D':
return 13;
case 'e':
case 'E':
return 14;
case 'f':
case 'F':
return 15;
default:
return 0;
}
}
#endregion
#region 四舍五入圆整
//四舍五入圆整,如 0.44 -> 0.4 , 0.46 -> 0.5//flag--小数点后位数
public static double Regulate(double v, int flag)
{
bool isNegative = false;
//如果是负数
if (v < 0)
{
isNegative = true;
v = Math.Abs(v);
}
else
{
isNegative = false;
}
double value = Math.Round(v, flag);
if (isNegative)
{
value = -value;
}
return value;
}
//四舍五入圆整,如 0.44 -> 0.4 , 0.46 -> 0.5//flag--小数点后位数
public static List Regulate(List inPoints, int flag)
{
if (inPoints == null)
return null;
List outPoints = new List();
for (int i = 0; i < inPoints.Count(); i++)
{
outPoints.Add(new IStation.Curve.CurvePoint(Regulate(inPoints[i].X, flag), Regulate(inPoints[i].Y, flag)));
}
return outPoints;
}
public static List Regulate(List inPoints, int flagX, int flagY)
{
if (inPoints == null)
return null;
List outPoints = new List();
for (int i = 0; i < inPoints.Count(); i++)
{
outPoints.Add(new IStation.Curve.CurvePoint(Regulate(inPoints[i].X, flagX), Regulate(inPoints[i].Y, flagY)));
}
return outPoints;
}
#endregion
#region 向上圆整
public static double RegulateUp(double m, int flag)
{
double k = 1;
for (int i = 0; i < flag; i++)
{
k = k * 10;
}
return System.Math.Ceiling(m * k) / k;
}
public static int RoundUp2(double v)
{
int k = (int)(v / 2) * 2 + 2;
return k;
}
public static int RoundUp5(double v)
{
int k = (int)(v / 5) * 5 + 5;
return k;
}
public static int RoundUp10(double v)
{
int k = (int)(v / 10) * 10 + 10;
return k;
}
#endregion
#region 向下圆整
public static double RegulateDown(double m, int flag)
{
double k = 1;
for (int i = 0; i < flag; i++)
{
k = k * 10;
}
return System.Math.Floor(m * k) / k;
}
public static int RoundDown2(double v)
{
int k = (int)(v / 2) * 2;
return k;
}
public static int RoundDown5(double v)
{
int k = (int)(v / 5) * 5;
return k;
}
public static int RoundDown10(double v)
{
int k = (int)(v / 10) * 10;
return k;
}
#endregion
#region 小数点长度
///
/// 获取小数点长度
///
public static int GetDecimalPlacesLength(double value)
{
string text = value.ToString();
int num = text.IndexOf('.');
if (num >= 0)
{
return text.Length - num - 1;
}
return 0;
}
///
/// 获取小数点长度
///
public static double GetByPlacesLength(double value, int length)
{
int decimalPlacesLength = GetDecimalPlacesLength(value);
if (decimalPlacesLength > length)
{
return Math.Round(value, length);
}
return value;
}
#endregion
}
}