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;
}
相關文章
相關標籤/搜索