using System;
using System.Collections.Generic;
using System.Text;
namespace Masuit.Tools
{
#pragma warning disable IDE0060 // 删除未使用的参数
public static class ValueTypeConvertExtensions
{ ///
/// 字符串转int
///
/// 源字符串
/// 失败时返回的值
/// int类型的数字
public static int ToInt32(this string s, int defaultValue = default)
{
int.TryParse(s, out defaultValue);
return defaultValue;
}
///
/// 字符串转long
///
/// 源字符串
/// 失败时返回的值
/// int类型的数字
public static long ToInt64(this string s, long defaultValue = default)
{
return s.ToLong(defaultValue);
}
///
/// 字符串转long类型
///
///
/// 转换失败的默认值
///
public static long ToLong(this string s, long defaultValue = default)
{
long.TryParse(s, out defaultValue);
return defaultValue;
}
///
/// 字符串转double
///
/// 源字符串
/// 失败时返回的值
/// double类型的数据
public static double ToDouble(this string s, double defaultValue = default)
{
double.TryParse(s, out defaultValue);
return defaultValue;
}
///
/// 字符串转decimal
///
/// 源字符串
/// 失败时返回的值
/// int类型的数字
public static decimal ToDecimal(this string s, decimal defaultValue = default)
{
decimal.TryParse(s, out defaultValue);
return defaultValue;
}
///
/// 字符串转decimal
///
/// 源字符串
/// int类型的数字
public static decimal ToDecimal(this double s)
{
return new decimal(s);
}
///
/// 字符串转double
///
/// 源字符串
/// double类型的数据
public static double ToDouble(this decimal s)
{
return (double)s;
}
///
/// 将double转换成int
///
/// double类型
/// int类型
public static int ToInt32(this double num)
{
return (int)Math.Floor(num);
}
///
/// 将double转换成int
///
/// double类型
/// int类型
public static int ToInt32(this decimal num)
{
return (int)Math.Floor(num);
}
///
/// 将int转换成double
///
/// int类型
/// int类型
public static double ToDouble(this int num)
{
return num * 1.0;
}
///
/// 将int转换成decimal
///
/// int类型
/// int类型
public static decimal ToDecimal(this int num)
{
return (decimal)(num * 1.0);
}
}
#pragma warning restore IDE0060 // 删除未使用的参数
}