using System;
using System.Collections.Generic;
using System.Text;
namespace CommonUtil
{
///
/// 扩展方法工具类
///
public static partial class ExtendUtil
{
#region bool 布尔值
///
/// 获取可空布尔值,支持转换0-1,是-否,yes-no
///
/// 数据
/// 可空布尔值
public static bool ToBool(this string data)
{
switch (data.ToLower())
{
case "0":
return false;
case "1":
return true;
case "是":
return true;
case "否":
return false;
case "yes":
return true;
case "no":
return false;
case "true":
return true;
case "false":
return false;
default:
return false;
}
}
///
/// 获取可空布尔值,支持转换0-1,是-否
///
/// 数据
/// 可空布尔值
public static bool ToBool(this T data) where T : struct
{
switch (data.ToInt())
{
case 0:
return false;
case 1:
return true;
default:
return false;
}
}
///
/// 获取可空布尔值,支持转换0-1,是-否
///
/// 数据
/// 可空布尔值
public static bool ToBool(this T? data) where T : struct
{
switch (data.ToInt())
{
case 0:
return false;
case 1:
return true;
default:
return false;
}
}
///
/// 转换为可空布尔值
///
/// 数据
/// 可空布尔值
public static bool? ToBoolOrNull(this string data)
{
if (data == null)
{
return null;
}
bool isValid = bool.TryParse(data, out bool result);
if (isValid)
{
return result;
}
return null;
}
///
/// 转换为可空布尔值
///
/// 数据
/// 可空布尔值
public static bool? ToBoolOrNull(this T data) where T : struct
{
return ToBoolOrNull(data.ToString());
}
///
/// 转换为可空布尔值
///
/// 数据
/// 可空布尔值
public static bool? ToBoolOrNull(this T? data) where T : struct
{
return ToBoolOrNull(SafeGetValue(data));
}
#endregion
#region string 字符串
///
/// 转换为字符串,null会转为string.Empty
///
/// 数据
/// 字符串
public static string ToStringEx(this object data)
{
return data == null ? string.Empty : data.ToString();
}
///
/// 加强版转换为字符串,并去除两边空格,null会转为string.Empty
///
/// 数据
/// 字符串
public static string ToStringExTrim(this object data)
{
return ToStringEx(data).Trim();
}
#endregion
///
/// 安全的返回值
///
/// 泛型
/// 数据
/// 泛型
public static T SafeGetValue(this T? data) where T : struct
{
return data ?? default(T);
}
///
/// 元转换为分
///
/// 元
/// 分
public static int ToFee(this double sender)
{
return (int)(sender * 100).ToDecimal();
}
///
/// 元转换为分
///
/// 元
/// 分
public static int ToFee(this decimal sender)
{
return (int)(sender * 100);
}
///
/// 可为空的元转换为分
///
/// 元
/// 分
public static int? ToFee(this double? sender)
{
return !sender.HasValue ? null : (int?)sender.Value.ToFee();
}
///
/// 可为空的元转换为分
///
/// 元
/// 分
public static int? ToFee(this decimal? sender)
{
return sender == null ? null : (int?)sender.Value.ToFee();
}
///
/// 分转换为元
///
/// 分
/// 元
public static double ByFee(this int fee)
{
return (double)(fee / 100.0M);
}
///
/// 可为空分转换为元
///
/// 分
/// 元
public static double? ByFee(this int? fee)
{
return fee == null ? null : (double?)fee.Value.ByFee();
}
///
/// 价格获取小数点后面的值(重构)
///
/// 价格数字分
/// 小数点后面的值
public static string ToPriceXiaoshu(this int? price)
{
return (price ?? 0).ToPriceXiaoshu();
}
///
/// 价格获取小数点后面的值
///
/// 价格数字分
/// 小数点后面的值
public static string ToPriceXiaoshu(this int price)
{
int xs = price % 100;
return xs < 10 ? ("0" + xs.ToString()) : xs.ToString();
}
///
/// 除法运算,得到百分数,会四舍五入
///
/// 被除数
/// 除数
/// 小数点后几位,默认4位
/// 除数为0时默认值,默认--
///
public static string ToPercent(decimal dividend, decimal divisor, int digital = 4, string defaultValue = "--")
{
if (divisor == 0)
{
return defaultValue;
}
string percent = (Math.Round(dividend / divisor, digital, MidpointRounding.AwayFromZero) * 100).ToString().TrimEnd('0').TrimEnd('.');
return $"{percent}%";
}
///
/// 字符串切割
///
///
///
///
public static string[] Split(this string data, char separatorChar)
{
if (string.IsNullOrEmpty(data))
{
return null;
}
return data.Split(new char[] { separatorChar }, StringSplitOptions.RemoveEmptyEntries);
}
///
/// 转换为驼峰格式
///
///
///
public static string ToCamelStyle(this string data)
{
if (string.IsNullOrEmpty(data))
{
return data;
}
char[] chars = data.ToCharArray();
StringBuilder buf = new StringBuilder(chars.Length);
for (int i = 0; i < chars.Length; i++)
{
if (i == 0)
{
buf.Append(char.ToLower(chars[i]));
}
else
{
buf.Append(chars[i]);
}
}
return buf.ToString();
}
///
/// 转换为 _ 形式
///
///
///
public static string ToUnderlineStyle(this string data)
{
if (string.IsNullOrEmpty(data))
{
return data;
}
StringBuilder buf = new StringBuilder();
for (int i = 0; i < data.Length; i++)
{
char c = data[i];
if (char.IsUpper(c))
{
if (i > 0)
{
buf.Append("_");
}
buf.Append(char.ToLower(c));
}
else
{
buf.Append(c);
}
}
return buf.ToString();
}
///
/// 清除字典中值为空的项。
///
/// 待清除的字典
/// 清除后的字典
public static IDictionary CleanupDictionary(this IDictionary dict)
{
IDictionary newDict = new Dictionary(dict.Count);
foreach (KeyValuePair kv in dict)
{
if (kv.Value != null)
{
newDict.Add(kv.Key, kv.Value);
}
}
return newDict;
}
}
}