/// <summary>Метод получения <see cref="NumberStyles"/> из значения в любом типе.</summary> /// <param name="value">Любое значение.</param> /// <param name="typeNumber">Тип для которого нужен <see cref="NumberStyles"/>.</param> /// <returns>Если в value значение в типе:<br/> /// <see cref="NumberStyles"/> - приводится к типу <see cref="NumberStyles"/>;<br/> /// <see cref="Enum"/> или целочисленный числовой тип - методами <see cref="Enum.ToObject(Type, object)"/> и <see cref="Enum.IsDefined(Type, object)"/> проверяет возможность преобразования в <see cref="NumberStyles"/>;<br/> /// <see cref="String"/> - парсинг методом <see cref="Enum.TryParse{TEnum}(string, bool, out TEnum)"/> с игнорированием регистра.<br/> /// Если не удалось получить из value <see cref="NumberStyles"/>, то возвращается значение по умолчанию для типа typeNumber:<br/> /// <see cref="Decimal"/> - <see cref="NumberStyles.Number"/>;<br/> /// <see cref="Double"/> или <see cref="Single"/> - <see cref="NumberStyles.Float"/> | <see cref="NumberStyles.AllowThousands"/>;<br/> /// для остальных - <see cref="NumberStyles.Integer"/>.</returns> public static NumberStyles GetNumberStyle(object value, Type typeNumber) { if (value != null) { if (value is NumberStyles style) { return(style); } if (IntegerTypes.Contains(value.GetType()) || value is Enum) { style = (NumberStyles)Enum.ToObject(typeof(NumberStyles), value); if (Enum.IsDefined(typeof(NumberStyles), style)) { return(style); } } else if (value is string str && Enum.TryParse(str, true, out style)) { return(style); } } // Стиль по умолчанию можно для всех типов возвращать NumberStyles.Any // Но не во всех случаях будет ожидаемый результат // return NumberStyles.Any; if (typeNumber == typeof(decimal)) { return(NumberStyles.Number); } if (FloatingPointTypes.Contains(typeNumber)) { return(NumberStyles.Float | NumberStyles.AllowThousands); } return(NumberStyles.Integer); }
public static bool IsFloatingPoint(this Type type) { Contract.Requires(type != null); return(FloatingPointTypes.Contains(type)); }
public static bool IsFloatingPointType(Type?type) { return(!(type is null) && FloatingPointTypes.Contains(type)); }
/// <summary> /// Check if the type is a floating point type. /// </summary> public static bool IsFloatingPoint(Type type) { return(FloatingPointTypes.Contains(type)); }