/// <exception cref="ArgumentOutOfRangeException"> /// <c>value</c> /// is out of range.</exception> public PxUnit(double value) { if (!value.InRange(MIN_VALUE, MAX_VALUE)) { throw new ArgumentOutOfRangeException(nameof(value)); } Value = (int)value; _type = PxUnitType.Pixel; }
/// <exception cref="ArgumentOutOfRangeException"> /// <c>value</c> /// is out of range.</exception> public PxUnit(double value, PxUnitType type) { if (!value.InRange(MIN_VALUE, MAX_VALUE)) { throw new ArgumentOutOfRangeException(nameof(value)); } Value = type == PxUnitType.Pixel ? Convert.ToInt32(value) : value; _type = type; }
private static string GetStringFromType(PxUnitType type) { switch (type) { case PxUnitType.Pixel: return("px"); case PxUnitType.Percentage: return("%"); } return(string.Empty); }
public PxUnit(Unit unit) { switch (unit.Type) { case UnitType.Percentage: Value = unit.Value; _type = PxUnitType.Percentage; break; default: Value = Convert.ToInt32(unit.Value); _type = PxUnitType.Pixel; break; } }
/// <exception cref="FormatException"></exception> /// <exception cref="ArgumentOutOfRangeException"> /// <c>value</c> /// is out of range.</exception> internal PxUnit(string value, CultureInfo culture, PxUnitType defaultType) { if (string.IsNullOrEmpty(value)) { Value = 0.0D; _type = 0; } else { culture ??= CultureInfo.CurrentUICulture; string str = value.Trim().ToLower(culture); int length = str.Length; int num2 = -1; for (int i = 0; i < length; i++) { char ch = str[i]; if ((ch < '0' || ch > '9') && ch != '-' && ch != '.' && ch != ',') { break; } num2 = i; } if (num2 == -1) { throw new FormatException($"Unit string '{value}' has no digits"); } _type = num2 < length - 1 ? GetTypeFromString(str.Substring(num2 + 1).Trim()) : defaultType; string text = str.Substring(0, num2 + 1); float v = Convert.ToSingle(text); if (!v.InRange(MIN_VALUE, MAX_VALUE)) { throw new ArgumentOutOfRangeException(nameof(value)); } Value = _type == PxUnitType.Pixel ? (int)v : v; } }