You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DS7/DSWeb.Common/Extentions/Int32Extensions.cs

91 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DSWeb.Common
{
/// <summary>
/// 扩展Int32功能
/// </summary>
public static class Int32Extensions
{
/// <summary>
/// 向上整除
/// 1.当num能被divideBy整除时,结果即为num/divideBy;
/// 2.当num不能被divideBy整除时,结果为num/divideBy + 1;
/// </summary>
/// <param name="num">被除数,大于或者等于0</param>
/// <param name="divideBy">除数,大于0</param>
/// <returns>向上整除结果</returns>
public static int CeilingDivide(this int num, int divideBy)
{
if (num < 0 || divideBy <= 0)
{
return 0;
}
return (num + divideBy - 1) / divideBy;
}
/// <summary>
/// 向上整除
/// 1.当num能被divideBy整除时,结果即为num/divideBy;
/// 2.当num不能被divideBy整除时,结果为num/divideBy + 1;
/// </summary>
/// <param name="num">被除数,大于或者等于0</param>
/// <param name="divideBy">除数,大于0</param>
/// <returns>向上整除结果</returns>
public static long CeilingDivide(this long num, long divideBy)
{
if (num < 0 || divideBy <= 0)
{
return 0;
}
return (num + divideBy - 1) / divideBy;
}
/// <summary>
/// 将List<int> 转为间隔字符串
/// </summary>
/// <param name="strlist"></param>
public static string ListToString(this List<int> strlist, char Split = ',')
{
StringBuilder str = new StringBuilder();
foreach (var item in strlist)
{
str.Append(item.ToString() + Split);
}
return str.ToString();
}
/// <summary>
/// 拓展 将字符串抓换为int
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static int StrToInt(this string str)
{
if (str.Isint())
{ return Convert.ToInt32(str); }
else
{
return 0;
}
}
/// <summary>
/// 拓展 将字符串数组转换成int 数组
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static int[] strToIntArray(this string[] str)
{
return Array.ConvertAll(str, new Converter<string, int>(StrToInt));
}
}
}