/// <summary> /// Convert to the unit representation <paramref name="unit" />. /// </summary> /// <returns>Value in new unit if successful, exception otherwise.</returns> /// <exception cref="NotImplementedException">If conversion was not successful.</exception> public double As(FlowUnit unit) { switch (unit) { case FlowUnit.CubicFootPerSecond: return(CubicFeetPerSecond); case FlowUnit.CubicMeterPerHour: return(CubicMetersPerHour); case FlowUnit.CubicMeterPerSecond: return(CubicMetersPerSecond); case FlowUnit.LitersPerMinute: return(LitersPerMinute); case FlowUnit.MillionUsGallonsPerDay: return(MillionUsGallonsPerDay); case FlowUnit.UsGallonsPerMinute: return(UsGallonsPerMinute); default: throw new NotImplementedException("unit: " + unit); } }
public string ToString(FlowUnit unit, [CanBeNull] IFormatProvider provider, int significantDigitsAfterRadix) { double value = As(unit); string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix); return(ToString(unit, provider, format)); }
public static string GetAbbreviation(FlowUnit unit, [CanBeNull] string cultureName) { // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); return(UnitSystem.GetCached(provider).GetDefaultAbbreviation(unit)); }
/// <summary> /// Dynamically convert from value and unit enum <see cref="FlowUnit" /> to <see cref="Flow" />. /// </summary> /// <param name="value">Value to convert from.</param> /// <param name="fromUnit">Unit to convert from.</param> /// <returns>Flow unit value.</returns> public static Flow?From(double?value, FlowUnit fromUnit) { if (!value.HasValue) { return(null); } switch (fromUnit) { case FlowUnit.CentilitersPerMinute: return(FromCentilitersPerMinute(value.Value)); case FlowUnit.CubicDecimeterPerMinute: return(FromCubicDecimetersPerMinute(value.Value)); case FlowUnit.CubicFootPerSecond: return(FromCubicFeetPerSecond(value.Value)); case FlowUnit.CubicMeterPerHour: return(FromCubicMetersPerHour(value.Value)); case FlowUnit.CubicMeterPerSecond: return(FromCubicMetersPerSecond(value.Value)); case FlowUnit.DecilitersPerMinute: return(FromDecilitersPerMinute(value.Value)); case FlowUnit.KilolitersPerMinute: return(FromKilolitersPerMinute(value.Value)); case FlowUnit.LitersPerHour: return(FromLitersPerHour(value.Value)); case FlowUnit.LitersPerMinute: return(FromLitersPerMinute(value.Value)); case FlowUnit.LitersPerSecond: return(FromLitersPerSecond(value.Value)); case FlowUnit.MicrolitersPerMinute: return(FromMicrolitersPerMinute(value.Value)); case FlowUnit.MillilitersPerMinute: return(FromMillilitersPerMinute(value.Value)); case FlowUnit.MillionUsGallonsPerDay: return(FromMillionUsGallonsPerDay(value.Value)); case FlowUnit.NanolitersPerMinute: return(FromNanolitersPerMinute(value.Value)); case FlowUnit.OilBarrelsPerDay: return(FromOilBarrelsPerDay(value.Value)); case FlowUnit.UsGallonsPerMinute: return(FromUsGallonsPerMinute(value.Value)); default: throw new NotImplementedException("fromUnit: " + fromUnit); } }
public string ToString(FlowUnit unit, [CanBeNull] Culture culture, int significantDigitsAfterRadix) { double value = As(unit); string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix); return(ToString(unit, culture, format)); }
/// <summary> /// Dynamically convert from value and unit enum <see cref="FlowUnit" /> to <see cref="Flow" />. /// </summary> /// <param name="value">Value to convert from.</param> /// <param name="fromUnit">Unit to convert from.</param> /// <returns>Flow unit value.</returns> public static Flow From(double value, FlowUnit fromUnit) { switch (fromUnit) { case FlowUnit.CubicFootPerSecond: return(FromCubicFeetPerSecond(value)); case FlowUnit.CubicMeterPerHour: return(FromCubicMetersPerHour(value)); case FlowUnit.CubicMeterPerSecond: return(FromCubicMetersPerSecond(value)); case FlowUnit.LitersPerMinute: return(FromLitersPerMinute(value)); case FlowUnit.MillionUsGallonsPerDay: return(FromMillionUsGallonsPerDay(value)); case FlowUnit.UsGallonsPerMinute: return(FromUsGallonsPerMinute(value)); default: throw new NotImplementedException("fromUnit: " + fromUnit); } }
/// <summary> /// Convert to the unit representation <paramref name="unit" />. /// </summary> /// <returns>Value in new unit if successful, exception otherwise.</returns> /// <exception cref="NotImplementedException">If conversion was not successful.</exception> public double As(FlowUnit unit) { switch (unit) { case FlowUnit.CentilitersPerMinute: return(CentilitersPerMinute); case FlowUnit.CubicDecimeterPerMinute: return(CubicDecimetersPerMinute); case FlowUnit.CubicFootPerHour: return(CubicFeetPerHour); case FlowUnit.CubicFootPerSecond: return(CubicFeetPerSecond); case FlowUnit.CubicMeterPerHour: return(CubicMetersPerHour); case FlowUnit.CubicMeterPerSecond: return(CubicMetersPerSecond); case FlowUnit.DecilitersPerMinute: return(DecilitersPerMinute); case FlowUnit.KilolitersPerMinute: return(KilolitersPerMinute); case FlowUnit.LitersPerHour: return(LitersPerHour); case FlowUnit.LitersPerMinute: return(LitersPerMinute); case FlowUnit.LitersPerSecond: return(LitersPerSecond); case FlowUnit.MicrolitersPerMinute: return(MicrolitersPerMinute); case FlowUnit.MillilitersPerMinute: return(MillilitersPerMinute); case FlowUnit.MillionUsGallonsPerDay: return(MillionUsGallonsPerDay); case FlowUnit.NanolitersPerMinute: return(NanolitersPerMinute); case FlowUnit.OilBarrelsPerDay: return(OilBarrelsPerDay); case FlowUnit.UsGallonsPerMinute: return(UsGallonsPerMinute); default: throw new NotImplementedException("unit: " + unit); } }
public string ToString(FlowUnit unit, CultureInfo culture, string format, params object[] args) { string abbreviation = UnitSystem.GetCached(culture).GetDefaultAbbreviation(unit); object[] finalArgs = new object[] { As(unit), abbreviation } .Concat(args) .ToArray(); return(string.Format(culture, format, finalArgs)); }
/// <summary> /// Convert to the unit representation <paramref name="unit" />. /// </summary> /// <returns>Value converted to the specified unit.</returns> public double As(FlowUnit unit) { if (Unit == unit) { return(Convert.ToDouble(Value)); } var converted = AsBaseNumericType(unit); return(Convert.ToDouble(converted)); }
/// <summary> /// Parse a string of the format "<quantity> <unit>". /// </summary> /// <example> /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// </example> /// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception> /// <exception cref="ArgumentException"> /// Expected 2 words. Input string needs to be in the format "<quantity> <unit /// >". /// </exception> /// <exception cref="UnitsNetException">Error parsing string.</exception> public static Flow Parse(string str, IFormatProvider formatProvider = null) { if (str == null) { throw new ArgumentNullException("str"); } var numFormat = formatProvider != null ? (NumberFormatInfo)formatProvider.GetFormat(typeof(NumberFormatInfo)) : NumberFormatInfo.CurrentInfo; var numRegex = string.Format(@"[\d., {0}{1}]*\d", // allows digits, dots, commas, and spaces in the quantity (must end in digit) numFormat.NumberGroupSeparator, // adds provided (or current) culture's group separator numFormat.NumberDecimalSeparator); // adds provided (or current) culture's decimal separator var regexString = string.Format("(?<value>[-+]?{0}{1}{2}{3}", numRegex, // capture base (integral) Quantity value @"(?:[eE][-+]?\d+)?)", // capture exponential (if any), end of Quantity capturing @"\s?", // ignore whitespace (allows both "1kg", "1 kg") @"(?<unit>\S+)"); // capture Unit (non-whitespace) input var regex = new Regex(regexString); GroupCollection groups = regex.Match(str.Trim()).Groups; var valueString = groups["value"].Value; var unitString = groups["unit"].Value; if (valueString == "" || unitString == "") { var ex = new ArgumentException( "Expected valid quantity and unit. Input string needs to be in the format \"<quantity><unit> or <quantity> <unit>\".", "str"); ex.Data["input"] = str; ex.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw ex; } try { FlowUnit unit = ParseUnit(unitString, formatProvider); double value = double.Parse(valueString, formatProvider); return(From(value, unit)); } catch (Exception e) { var newEx = new UnitsNetException("Error parsing string.", e); newEx.Data["input"] = str; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } }
/// <summary> /// Dynamically convert from value and unit enum <see cref="FlowUnit" /> to <see cref="Flow" />. /// </summary> /// <param name="value">Value to convert from.</param> /// <param name="fromUnit">Unit to convert from.</param> /// <returns>Flow unit value.</returns> public static Flow From(double value, FlowUnit fromUnit) { switch (fromUnit) { case FlowUnit.CubicMeterPerHour: return(FromCubicMetersPerHour(value)); case FlowUnit.CubicMeterPerSecond: return(FromCubicMetersPerSecond(value)); default: throw new NotImplementedException("fromUnit: " + fromUnit); } }
/// <summary> /// Convert to the unit representation <paramref name="unit" />. /// </summary> /// <returns>Value in new unit if successful, exception otherwise.</returns> /// <exception cref="NotImplementedException">If conversion was not successful.</exception> public double As(FlowUnit unit) { switch (unit) { case FlowUnit.CubicMeterPerHour: return(CubicMetersPerHour); case FlowUnit.CubicMeterPerSecond: return(CubicMetersPerSecond); default: throw new NotImplementedException("unit: " + unit); } }
/// <summary> /// Parse a string given a particular regular expression. /// </summary> /// <exception cref="UnitsNetException">Error parsing string.</exception> private static List <Flow> ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List <Flow>(); foreach (Match match in matches) { GroupCollection groups = match.Groups; var valueString = groups["value"].Value; var unitString = groups["unit"].Value; if (groups["invalid"].Value != "") { var newEx = new UnitsNetException("Invalid string detected: " + groups["invalid"].Value); newEx.Data["input"] = str; newEx.Data["matched value"] = valueString; newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } if (valueString == "" && unitString == "") { continue; } try { FlowUnit unit = ParseUnit(unitString, formatProvider); double value = double.Parse(valueString, formatProvider); converted.Add(From(value, unit)); } catch (AmbiguousUnitParseException ambiguousException) { throw; } catch (Exception ex) { var newEx = new UnitsNetException("Error parsing string.", ex); newEx.Data["input"] = str; newEx.Data["matched value"] = valueString; newEx.Data["matched unit"] = unitString; newEx.Data["formatprovider"] = formatProvider == null ? null : formatProvider.ToString(); throw newEx; } } return(converted); }
/// <summary> /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// </summary> /// <param name="str">String to parse. Typically in the form: {number} {unit}</param> /// <param name="provider">Format to use when parsing number and unit. Defaults to <see cref="UnitSystem.DefaultCulture" />.</param> /// <example> /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// </example> /// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception> /// <exception cref="ArgumentException"> /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// </exception> /// <exception cref="AmbiguousUnitParseException"> /// More than one unit is represented by the specified unit abbreviation. /// Example: Volume.Parse("1 cup") will throw, because it can refer to any of /// <see cref="VolumeUnit.MetricCup" />, <see cref="VolumeUnit.UsLegalCup" /> and <see cref="VolumeUnit.UsCustomaryCup" />. /// </exception> /// <exception cref="UnitsNetException"> /// If anything else goes wrong, typically due to a bug or unhandled case. /// We wrap exceptions in <see cref="UnitsNetException" /> to allow you to distinguish /// Units.NET exceptions from other exceptions. /// </exception> public static Flow Parse(string str, [CanBeNull] IFormatProvider provider) { if (str == null) { throw new ArgumentNullException(nameof(str)); } provider = provider ?? UnitSystem.DefaultCulture; return(QuantityParser.Parse <Flow, FlowUnit>(str, provider, delegate(string value, string unit, IFormatProvider formatProvider2) { double parsedValue = double.Parse(value, formatProvider2); FlowUnit parsedUnit = ParseUnit(unit, formatProvider2); return From(parsedValue, parsedUnit); }, (x, y) => FromCubicMetersPerSecond(x.CubicMetersPerSecond + y.CubicMetersPerSecond))); }
/// <summary> /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// </summary> /// <param name="str">String to parse. Typically in the form: {number} {unit}</param> /// <param name="cultureName">Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to <see cref="UnitSystem" />'s default culture.</param> /// <example> /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// </example> /// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception> /// <exception cref="ArgumentException"> /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// </exception> /// <exception cref="AmbiguousUnitParseException"> /// More than one unit is represented by the specified unit abbreviation. /// Example: Volume.Parse("1 cup") will throw, because it can refer to any of /// <see cref="VolumeUnit.MetricCup" />, <see cref="VolumeUnit.UsLegalCup" /> and <see cref="VolumeUnit.UsCustomaryCup" />. /// </exception> /// <exception cref="UnitsNetException"> /// If anything else goes wrong, typically due to a bug or unhandled case. /// We wrap exceptions in <see cref="UnitsNetException" /> to allow you to distinguish /// Units.NET exceptions from other exceptions. /// </exception> public static Flow Parse(string str, [CanBeNull] string cultureName) { if (str == null) { throw new ArgumentNullException(nameof(str)); } // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); return(QuantityParser.Parse <Flow, FlowUnit>(str, provider, delegate(string value, string unit, IFormatProvider formatProvider2) { double parsedValue = double.Parse(value, formatProvider2); FlowUnit parsedUnit = ParseUnit(unit, formatProvider2); return From(parsedValue, parsedUnit); }, (x, y) => FromCubicMetersPerSecond(x.CubicMetersPerSecond + y.CubicMetersPerSecond))); }
public string ToString(FlowUnit unit, [CanBeNull] IFormatProvider provider, [NotNull] string format, [NotNull] params object[] args) { if (format == null) { throw new ArgumentNullException(nameof(format)); } if (args == null) { throw new ArgumentNullException(nameof(args)); } provider = provider ?? UnitSystem.DefaultCulture; double value = As(unit); object[] formatArgs = UnitFormatter.GetFormatArgs(unit, value, provider, args); return(string.Format(provider, format, formatArgs)); }
public string ToString(FlowUnit unit, [CanBeNull] string cultureName, [NotNull] string format, [NotNull] params object[] args) { if (format == null) { throw new ArgumentNullException(nameof(format)); } if (args == null) { throw new ArgumentNullException(nameof(args)); } // Windows Runtime Component does not support CultureInfo and IFormatProvider types, so we use culture name for public methods: https://msdn.microsoft.com/en-us/library/br230301.aspx IFormatProvider provider = cultureName == null ? UnitSystem.DefaultCulture : new CultureInfo(cultureName); double value = As(unit); object[] formatArgs = UnitFormatter.GetFormatArgs(unit, value, provider, args); return(string.Format(provider, format, formatArgs)); }
/// <summary> /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// </summary> /// <param name="str">String to parse. Typically in the form: {number} {unit}</param> /// <param name="culture">Format to use when parsing number and unit. If it is null, it defaults to <see cref="NumberFormatInfo.CurrentInfo"/> for parsing the number and <see cref="CultureInfo.CurrentUICulture"/> for parsing the unit abbreviation by culture/language.</param> /// <example> /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// </example> /// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception> /// <exception cref="ArgumentException"> /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// </exception> /// <exception cref="AmbiguousUnitParseException"> /// More than one unit is represented by the specified unit abbreviation. /// Example: Volume.Parse("1 cup") will throw, because it can refer to any of /// <see cref="VolumeUnit.MetricCup" />, <see cref="VolumeUnit.UsLegalCup" /> and <see cref="VolumeUnit.UsCustomaryCup" />. /// </exception> /// <exception cref="UnitsNetException"> /// If anything else goes wrong, typically due to a bug or unhandled case. /// We wrap exceptions in <see cref="UnitsNetException" /> to allow you to distinguish /// Units.NET exceptions from other exceptions. /// </exception> public static Flow Parse(string str, [CanBeNull] Culture culture) { if (str == null) { throw new ArgumentNullException("str"); } #if WINDOWS_UWP IFormatProvider formatProvider = culture == null ? null : new CultureInfo(culture); #else IFormatProvider formatProvider = culture; #endif return(UnitParser.ParseUnit <Flow>(str, formatProvider, delegate(string value, string unit, IFormatProvider formatProvider2) { double parsedValue = double.Parse(value, formatProvider2); FlowUnit parsedUnit = ParseUnit(unit, formatProvider2); return From(parsedValue, parsedUnit); }, (x, y) => FromCubicMetersPerSecond(x.CubicMetersPerSecond + y.CubicMetersPerSecond))); }
public string ToString(FlowUnit unit, [CanBeNull] Culture culture, [NotNull] string format, [NotNull] params object[] args) { if (format == null) { throw new ArgumentNullException(nameof(format)); } if (args == null) { throw new ArgumentNullException(nameof(args)); } #if WINDOWS_UWP IFormatProvider formatProvider = culture == null ? null : new CultureInfo(culture); #else IFormatProvider formatProvider = culture; #endif double value = As(unit); object[] formatArgs = UnitFormatter.GetFormatArgs(unit, value, formatProvider, args); return(string.Format(formatProvider, format, formatArgs)); }
/// <summary> /// Parse a string with one or two quantities of the format "<quantity> <unit>". /// </summary> /// <param name="str">String to parse. Typically in the form: {number} {unit}</param> /// <param name="culture">Format to use when parsing number and unit. If it is null, it defaults to <see cref="NumberFormatInfo.CurrentInfo"/> for parsing the number and <see cref="CultureInfo.CurrentUICulture"/> for parsing the unit abbreviation by culture/language.</param> /// <example> /// Length.Parse("5.5 m", new CultureInfo("en-US")); /// </example> /// <exception cref="ArgumentNullException">The value of 'str' cannot be null. </exception> /// <exception cref="ArgumentException"> /// Expected string to have one or two pairs of quantity and unit in the format /// "<quantity> <unit>". Eg. "5.5 m" or "1ft 2in" /// </exception> /// <exception cref="AmbiguousUnitParseException"> /// More than one unit is represented by the specified unit abbreviation. /// Example: Volume.Parse("1 cup") will throw, because it can refer to any of /// <see cref="VolumeUnit.MetricCup" />, <see cref="VolumeUnit.UsLegalCup" /> and <see cref="VolumeUnit.UsCustomaryCup" />. /// </exception> /// <exception cref="UnitsNetException"> /// If anything else goes wrong, typically due to a bug or unhandled case. /// We wrap exceptions in <see cref="UnitsNetException" /> to allow you to distinguish /// Units.NET exceptions from other exceptions. /// </exception> public static Flow Parse(string str, [CanBeNull] Culture culture) { if (str == null) { throw new ArgumentNullException("str"); } // Windows Runtime Component does not support CultureInfo type, so use culture name string for public methods instead: https://msdn.microsoft.com/en-us/library/br230301.aspx #if WINDOWS_UWP IFormatProvider formatProvider = culture == null ? null : new CultureInfo(culture); #else IFormatProvider formatProvider = culture; #endif return(QuantityParser.Parse <Flow, FlowUnit>(str, formatProvider, delegate(string value, string unit, IFormatProvider formatProvider2) { double parsedValue = double.Parse(value, formatProvider2); FlowUnit parsedUnit = ParseUnit(unit, formatProvider2); return From(parsedValue, parsedUnit); }, (x, y) => FromCubicMetersPerSecond(x.CubicMetersPerSecond + y.CubicMetersPerSecond))); }
public string ToString(FlowUnit unit, [CanBeNull] Culture culture, [NotNull] string format, [NotNull] params object[] args) { if (format == null) { throw new ArgumentNullException(nameof(format)); } if (args == null) { throw new ArgumentNullException(nameof(args)); } // Windows Runtime Component does not support CultureInfo type, so use culture name string for public methods instead: https://msdn.microsoft.com/en-us/library/br230301.aspx #if WINDOWS_UWP IFormatProvider formatProvider = culture == null ? null : new CultureInfo(culture); #else IFormatProvider formatProvider = culture; #endif double value = As(unit); object[] formatArgs = UnitFormatter.GetFormatArgs(unit, value, formatProvider, args); return(string.Format(formatProvider, format, formatArgs)); }
public static string GetAbbreviation( FlowUnit unit, #if WINDOWS_UWP [CanBeNull] string cultureName)
public static string GetAbbreviation(FlowUnit unit) { return(GetAbbreviation(unit, null)); }
public static Flow From(QuantityValue value, FlowUnit fromUnit) #endif { return(new Flow((double)value, fromUnit)); }
public static Flow From(double value, FlowUnit fromUnit)
Flow(double numericValue, FlowUnit unit) { _value = numericValue; _unit = unit; }
public string ToString(FlowUnit unit, CultureInfo culture, string format, params object[] args) { return(string.Format(culture, format, UnitFormatter.GetFormatArgs(unit, As(unit), culture, args))); }
public static string GetAbbreviation(FlowUnit unit, [CanBeNull] Culture culture) { return(UnitSystem.GetCached(culture).GetDefaultAbbreviation(unit)); }
/// <summary> /// Get string representation of value and unit. Using two significant digits after radix. /// </summary> /// <param name="unit">Unit representation to use.</param> /// <param name="culture">Culture to use for localization and number formatting.</param> /// <returns>String representation.</returns> public string ToString(FlowUnit unit, [CanBeNull] Culture culture) { return(ToString(unit, culture, 2)); }
/// <summary> /// Get string representation of value and unit. Using current UI culture and two significant digits after radix. /// </summary> /// <param name="unit">Unit representation to use.</param> /// <returns>String representation.</returns> public string ToString(FlowUnit unit) { return(ToString(unit, null, 2)); }