/// <devdoc> /// Converts UnitType to persistence string. /// </devdoc> private static string GetStringFromType(WikiPlex.Legacy.UnitType type) { switch (type) { case WikiPlex.Legacy.UnitType.Pixel: return("px"); case WikiPlex.Legacy.UnitType.Point: return("pt"); case WikiPlex.Legacy.UnitType.Pica: return("pc"); case WikiPlex.Legacy.UnitType.Inch: return("in"); case WikiPlex.Legacy.UnitType.Mm: return("mm"); case WikiPlex.Legacy.UnitType.Cm: return("cm"); case WikiPlex.Legacy.UnitType.Percentage: return("%"); case WikiPlex.Legacy.UnitType.Em: return("em"); case WikiPlex.Legacy.UnitType.Ex: return("ex"); } return(string.Empty); }
/// <devdoc> /// <para> Initializes a new instance of the <see cref='System.Web.UI.WebControls.Unit'/> structure with the /// specified double-precision /// floating point number as the unit value and <see langword='Pixel'/> /// as the (default) unit type.</para> /// </devdoc> public Unit(double value) { if ((value < MinValue) || (value > MaxValue)) { throw new System.ArgumentOutOfRangeException("value"); } this.value = (int)value; this.type = WikiPlex.Legacy.UnitType.Pixel; }
internal Unit(string value, System.Globalization.CultureInfo culture, WikiPlex.Legacy.UnitType defaultType) { if (string.IsNullOrEmpty(value)) { this.value = 0; this.type = (WikiPlex.Legacy.UnitType) 0; } else { if (culture == null) { culture = System.Globalization.CultureInfo.CurrentCulture; } // This is invariant because it acts like an enum with a number together. // The enum part is invariant, but the number uses current culture. string trimLcase = value.Trim().ToLower(System.Globalization.CultureInfo.InvariantCulture); int len = trimLcase.Length; int lastDigit = -1; for (int i = 0; i < len; i++) { char ch = trimLcase[i]; if (((ch < '0') || (ch > '9')) && (ch != '-') && (ch != '.') && (ch != ',')) { break; } lastDigit = i; } if (lastDigit == -1) { throw new System.FormatException("\"" + value + "\" is not digit..."); } if (lastDigit < len - 1) { type = (WikiPlex.Legacy.UnitType)GetTypeFromString(trimLcase.Substring(lastDigit + 1).Trim()); } else { type = defaultType; } string numericPart = trimLcase.Substring(0, lastDigit + 1); // Cannot use Double.FromString, because we don't use it in the ToString implementation try { System.ComponentModel.TypeConverter converter = new System.ComponentModel.SingleConverter(); this.value = (System.Single)converter.ConvertFromString(null, culture, numericPart); if (type == WikiPlex.Legacy.UnitType.Pixel) { this.value = (int)this.value; } } catch { throw new System.FormatException("\"" + value + "\" UnitParseNumericPart \"" + numericPart + "\" Type: " + type.ToString("G")); } if ((this.value < MinValue) || (this.value > MaxValue)) { throw new System.ArgumentOutOfRangeException("value"); } } }