using System;
using System.Collections.Generic;
using System.Text;
namespace CommonUtil
{
///
/// 扩展方法工具类-双精度浮点数
///
public static partial class ExtendUtil
{
///
/// 数据转换为双精度浮点数,null会转为0!
///
/// 数据
/// 双精度浮点数
public static double ToDouble(this string data)
{
if (data.IsNullOrEmpty())
{
return 0;
}
return double.TryParse(data, out double result) ? result : 0;
}
///
/// 数据转换为双精度浮点数,null会转为0!
///
/// 数据
/// 双精度浮点数
public static double ToDouble(this T data) where T : struct
{
return double.TryParse(data.ToStringEx(), out double result) ? result : 0;
}
///
/// 数据转换为双精度浮点数,null会转为0!
///
/// 数据
/// 双精度浮点数
public static double ToDouble(this T? data) where T : struct
{
return data.HasValue ? ToDouble(data.Value) : 0;
}
///
/// 转换为双精度浮点数,并按指定的位数四舍五入,null会转为0!
///
/// 数据
/// 小数位数
/// 双精度浮点数
public static double ToDouble(this string data, int digits)
{
return Math.Round(ToDouble(data), digits, MidpointRounding.AwayFromZero);
}
///
/// 转换为双精度浮点数,并按指定的位数四舍五入,null会转为0!
///
/// 数据
/// 小数位数
/// 双精度浮点数
public static double ToDouble(this T data, int digits) where T : struct
{
return Math.Round(ToDouble(data), digits, MidpointRounding.AwayFromZero);
}
///
/// 转换为双精度浮点数,并按指定的位数四舍五入,null会转为0!
///
/// 数据
/// 小数位数
/// 双精度浮点数
public static double ToDouble(this T? data, int digits) where T : struct
{
return data.HasValue ? ToDouble(data.Value, digits) : 0;
}
///
/// 转换为可空的双精度浮点数
///
/// 数据
/// 可空的双精度浮点数
public static double? ToDoubleOrNull(this string data)
{
if (data == null)
{
return null;
}
bool isValid = double.TryParse(data.ToString(), out double result);
if (isValid)
{
return result;
}
return null;
}
///
/// 转换为可空的双精度浮点数
///
/// 数据
/// 可空的双精度浮点数
public static double? ToDoubleOrNull(this T data) where T : struct
{
bool isValid = double.TryParse(data.ToString(), out double result);
if (isValid)
{
return result;
}
return null;
}
///
/// 转换为可空的双精度浮点数
///
/// 数据
/// 可空的双精度浮点数
public static double? ToDoubleOrNull(this T? data) where T : struct
{
return data.HasValue ? ToDoubleOrNull(data.Value) : null;
}
}
}