using System.Text.RegularExpressions;

namespace WmsMobileServe.Utils;

public static class StringUtils
{
    /// <summary>
    /// 判断一个字符串是不是 decimal
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static bool IsDecimal(this string? value)
    {
        if(string.IsNullOrWhiteSpace(value)) return false;
        return Regex.IsMatch(value, "^\\d+\\.?\\d+$");
    }

    /// <summary>
    /// 判断一个字符串 是不是 decimal
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static bool IsNotDecimal(this string? value)
    {
        return !value.IsDecimal();
    }

    /// <summary>
    /// 将字符串转换为 decimal
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static decimal? ToDecimal(this string? value)
    {
        try
        {
            if (!value.IsDecimal()) return default;
            return Convert.ToDecimal(value);
        }
        catch { return default; }
    }

    public static bool IsNumber(this string? value)
    {
        if (string.IsNullOrWhiteSpace(value)) return false;
        return Regex.IsMatch(value, "^\\d+$");
    }


}
