C# 字符串扩展去除千分位,支持设置精确度

/// <summary>
/// 字符串去除千分位,可以指定精度,默认为两位小数,若失败字符串为0
/// </summary>
/// <param name="This"></param>
/// <param name="accuracy"></param>
/// <returns></returns>
public static String  ParseThousands(this String This, int accuracy = 2)
{
    if (String.IsNullOrEmpty(This))
        return This;

    try
    {
        This = double.Parse(This, NumberStyles.AllowThousands | NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign).ToString(String.Format("F{0}", accuracy));
    }
    catch (Exception)
    {
        This = 0.ToString(String.Format("F{0}", accuracy));
    }

    return This;
}
posted @ 2020-09-14 15:26  Evilloafer  阅读(56)  评论(0编辑  收藏