public string ToString(TemperatureChangeRateUnit unit, [CanBeNull] string cultureName, int significantDigitsAfterRadix) { double value = As(unit); string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix); return(ToString(unit, cultureName, format)); }
/// <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(TemperatureChangeRateUnit unit) { switch (unit) { case TemperatureChangeRateUnit.CentidegreeCelsiusPerSecond: return(CentidegreesCelsiusPerSecond); case TemperatureChangeRateUnit.DecadegreeCelsiusPerSecond: return(DecadegreesCelsiusPerSecond); case TemperatureChangeRateUnit.DecidegreeCelsiusPerSecond: return(DecidegreesCelsiusPerSecond); case TemperatureChangeRateUnit.DegreeCelsiusPerSecond: return(DegreesCelsiusPerSecond); case TemperatureChangeRateUnit.HectodegreeCelsiusPerSecond: return(HectodegreesCelsiusPerSecond); case TemperatureChangeRateUnit.KilodegreeCelsiusPerSecond: return(KilodegreesCelsiusPerSecond); case TemperatureChangeRateUnit.MicrodegreeCelsiusPerSecond: return(MicrodegreesCelsiusPerSecond); case TemperatureChangeRateUnit.MillidegreeCelsiusPerSecond: return(MillidegreesCelsiusPerSecond); case TemperatureChangeRateUnit.NanodegreeCelsiusPerSecond: return(NanodegreesCelsiusPerSecond); default: throw new NotImplementedException("unit: " + unit); } }
public static string GetAbbreviation(TemperatureChangeRateUnit 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="TemperatureChangeRateUnit" /> to <see cref="TemperatureChangeRate" />. /// </summary> /// <param name="val">Value to convert from.</param> /// <param name="fromUnit">Unit to convert from.</param> /// <returns>TemperatureChangeRate unit value.</returns> public static TemperatureChangeRate From(double val, TemperatureChangeRateUnit fromUnit) { switch (fromUnit) { case TemperatureChangeRateUnit.CentidegreeCelsiusPerSecond: return(FromCentidegreesCelsiusPerSecond(val)); case TemperatureChangeRateUnit.DecadegreeCelsiusPerSecond: return(FromDecadegreesCelsiusPerSecond(val)); case TemperatureChangeRateUnit.DecidegreeCelsiusPerSecond: return(FromDecidegreesCelsiusPerSecond(val)); case TemperatureChangeRateUnit.DegreeCelsiusPerSecond: return(FromDegreesCelsiusPerSecond(val)); case TemperatureChangeRateUnit.HectodegreeCelsiusPerSecond: return(FromHectodegreesCelsiusPerSecond(val)); case TemperatureChangeRateUnit.KilodegreeCelsiusPerSecond: return(FromKilodegreesCelsiusPerSecond(val)); case TemperatureChangeRateUnit.MicrodegreeCelsiusPerSecond: return(FromMicrodegreesCelsiusPerSecond(val)); case TemperatureChangeRateUnit.MillidegreeCelsiusPerSecond: return(FromMillidegreesCelsiusPerSecond(val)); case TemperatureChangeRateUnit.NanodegreeCelsiusPerSecond: return(FromNanodegreesCelsiusPerSecond(val)); default: throw new NotImplementedException("fromUnit: " + fromUnit); } }
private decimal AsBaseNumericType(TemperatureChangeRateUnit unit) { if (Unit == unit) { return(_value); } var baseUnitValue = AsBaseUnit(); switch (unit) { case TemperatureChangeRateUnit.CentidegreeCelsiusPerSecond: return((baseUnitValue) / 1e-2m); case TemperatureChangeRateUnit.DecadegreeCelsiusPerSecond: return((baseUnitValue) / 1e1m); case TemperatureChangeRateUnit.DecidegreeCelsiusPerSecond: return((baseUnitValue) / 1e-1m); case TemperatureChangeRateUnit.DegreeCelsiusPerMinute: return(baseUnitValue * 60m); case TemperatureChangeRateUnit.DegreeCelsiusPerSecond: return(baseUnitValue); case TemperatureChangeRateUnit.HectodegreeCelsiusPerSecond: return((baseUnitValue) / 1e2m); case TemperatureChangeRateUnit.KilodegreeCelsiusPerSecond: return((baseUnitValue) / 1e3m); case TemperatureChangeRateUnit.MicrodegreeCelsiusPerSecond: return((baseUnitValue) / 1e-6m); case TemperatureChangeRateUnit.MillidegreeCelsiusPerSecond: return((baseUnitValue) / 1e-3m); case TemperatureChangeRateUnit.NanodegreeCelsiusPerSecond: return((baseUnitValue) / 1e-9m); default: throw new NotImplementedException($"Can not convert {Unit} to {unit}."); } }
private double GetValueAs(TemperatureChangeRateUnit unit) { if (Unit == unit) { return(_value); } var baseUnitValue = GetValueInBaseUnit(); switch (unit) { case TemperatureChangeRateUnit.CentidegreeCelsiusPerSecond: return((baseUnitValue) / 1e-2d); case TemperatureChangeRateUnit.DecadegreeCelsiusPerSecond: return((baseUnitValue) / 1e1d); case TemperatureChangeRateUnit.DecidegreeCelsiusPerSecond: return((baseUnitValue) / 1e-1d); case TemperatureChangeRateUnit.DegreeCelsiusPerMinute: return(baseUnitValue * 60); case TemperatureChangeRateUnit.DegreeCelsiusPerSecond: return(baseUnitValue); case TemperatureChangeRateUnit.HectodegreeCelsiusPerSecond: return((baseUnitValue) / 1e2d); case TemperatureChangeRateUnit.KilodegreeCelsiusPerSecond: return((baseUnitValue) / 1e3d); case TemperatureChangeRateUnit.MicrodegreeCelsiusPerSecond: return((baseUnitValue) / 1e-6d); case TemperatureChangeRateUnit.MillidegreeCelsiusPerSecond: return((baseUnitValue) / 1e-3d); case TemperatureChangeRateUnit.NanodegreeCelsiusPerSecond: return((baseUnitValue) / 1e-9d); default: throw new NotImplementedException($"Can not convert {Unit} to {unit}."); } }
private double AsBaseNumericType(TemperatureChangeRateUnit unit) { if (Unit == unit) { return(_value); } var baseUnitValue = AsBaseUnit(); switch (unit) { case TemperatureChangeRateUnit.CentidegreeCelsiusPerSecond: return((baseUnitValue) / 1e-2d); case TemperatureChangeRateUnit.DecadegreeCelsiusPerSecond: return((baseUnitValue) / 1e1d); case TemperatureChangeRateUnit.DecidegreeCelsiusPerSecond: return((baseUnitValue) / 1e-1d); case TemperatureChangeRateUnit.DegreeCelsiusPerMinute: return(baseUnitValue * 60); case TemperatureChangeRateUnit.DegreeCelsiusPerSecond: return(baseUnitValue); case TemperatureChangeRateUnit.ExadegreeCelsiusPerSecond: return((baseUnitValue) / 1e18d); case TemperatureChangeRateUnit.FemtodegreeCelsiusPerSecond: return((baseUnitValue) / 1e-15d); case TemperatureChangeRateUnit.GigadegreeCelsiusPerSecond: return((baseUnitValue) / 1e9d); case TemperatureChangeRateUnit.HectodegreeCelsiusPerSecond: return((baseUnitValue) / 1e2d); case TemperatureChangeRateUnit.KilodegreeCelsiusPerSecond: return((baseUnitValue) / 1e3d); case TemperatureChangeRateUnit.MegadegreeCelsiusPerSecond: return((baseUnitValue) / 1e6d); case TemperatureChangeRateUnit.MicrodegreeCelsiusPerSecond: return((baseUnitValue) / 1e-6d); case TemperatureChangeRateUnit.MillidegreeCelsiusPerSecond: return((baseUnitValue) / 1e-3d); case TemperatureChangeRateUnit.NanodegreeCelsiusPerSecond: return((baseUnitValue) / 1e-9d); case TemperatureChangeRateUnit.PetadegreeCelsiusPerSecond: return((baseUnitValue) / 1e15d); case TemperatureChangeRateUnit.PicodegreeCelsiusPerSecond: return((baseUnitValue) / 1e-12d); case TemperatureChangeRateUnit.QutradegreeCelsiusPerSecond: return((baseUnitValue) / 1e27d); case TemperatureChangeRateUnit.TeradegreeCelsiusPerSecond: return((baseUnitValue) / 1e12d); case TemperatureChangeRateUnit.VettadegreeCelsiusPerSecond: return((baseUnitValue) / 1e30d); case TemperatureChangeRateUnit.YottadegreeCelsiusPerSecond: return((baseUnitValue) / 1e24d); case TemperatureChangeRateUnit.ZettadegreeCelsiusPerSecond: return((baseUnitValue) / 1e21d); default: throw new NotImplementedException($"Can not convert {Unit} to {unit}."); } }
/// <summary> /// Creates the quantity with the given numeric value and unit. /// </summary> /// <param name="numericValue">The numeric value to contruct this quantity with.</param> /// <param name="unit">The unit representation to contruct this quantity with.</param> /// <remarks>Value parameter cannot be named 'value' due to constraint when targeting Windows Runtime Component.</remarks> /// <exception cref="ArgumentException">If value is NaN or Infinity.</exception> private TemperatureChangeRate(double numericValue, TemperatureChangeRateUnit unit) { if (unit == TemperatureChangeRateUnit.Undefined) { throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit)); } _value = Guard.EnsureValidNumber(numericValue, nameof(numericValue)); _unit = unit; }
/// <summary> /// Creates the quantity with the given numeric value and unit. /// </summary> /// <param name="value">The numeric value to construct this quantity with.</param> /// <param name="unit">The unit representation to construct this quantity with.</param> /// <remarks>Value parameter cannot be named 'value' due to constraint when targeting Windows Runtime Component.</remarks> /// <exception cref="ArgumentException">If value is NaN or Infinity.</exception> private TemperatureChangeRate(decimal value, TemperatureChangeRateUnit unit) { if (unit == TemperatureChangeRateUnit.Undefined) { throw new ArgumentException("The quantity can not be created with an undefined unit.", nameof(unit)); } _value = value; _unit = unit; }
/// <summary> /// Convert to the unit representation <paramref name="unit" />. /// </summary> /// <returns>Value converted to the specified unit.</returns> public decimal As(TemperatureChangeRateUnit unit) { if (Unit == unit) { return(Convert.ToDecimal(Value)); } var converted = AsBaseNumericType(unit); return(Convert.ToDecimal(converted)); }
/// <summary> /// Parse a string given a particular regular expression. /// </summary> /// <exception cref="UnitsNetException">Error parsing string.</exception> private static List <TemperatureChangeRate> ParseWithRegex(string regexString, string str, IFormatProvider formatProvider = null) { var regex = new Regex(regexString); MatchCollection matches = regex.Matches(str.Trim()); var converted = new List <TemperatureChangeRate>(); 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 { TemperatureChangeRateUnit 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); }
// ReSharper restore VirtualMemberNeverOverriden.Global protected (double UnitsInBaseUnit, double Tolerence) GetConversionFactor(TemperatureChangeRateUnit unit) { return(unit switch { TemperatureChangeRateUnit.CentidegreeCelsiusPerSecond => (CentidegreesCelsiusPerSecondInOneDegreeCelsiusPerSecond, CentidegreesCelsiusPerSecondTolerance), TemperatureChangeRateUnit.DecadegreeCelsiusPerSecond => (DecadegreesCelsiusPerSecondInOneDegreeCelsiusPerSecond, DecadegreesCelsiusPerSecondTolerance), TemperatureChangeRateUnit.DecidegreeCelsiusPerSecond => (DecidegreesCelsiusPerSecondInOneDegreeCelsiusPerSecond, DecidegreesCelsiusPerSecondTolerance), TemperatureChangeRateUnit.DegreeCelsiusPerMinute => (DegreesCelsiusPerMinuteInOneDegreeCelsiusPerSecond, DegreesCelsiusPerMinuteTolerance), TemperatureChangeRateUnit.DegreeCelsiusPerSecond => (DegreesCelsiusPerSecondInOneDegreeCelsiusPerSecond, DegreesCelsiusPerSecondTolerance), TemperatureChangeRateUnit.HectodegreeCelsiusPerSecond => (HectodegreesCelsiusPerSecondInOneDegreeCelsiusPerSecond, HectodegreesCelsiusPerSecondTolerance), TemperatureChangeRateUnit.KilodegreeCelsiusPerSecond => (KilodegreesCelsiusPerSecondInOneDegreeCelsiusPerSecond, KilodegreesCelsiusPerSecondTolerance), TemperatureChangeRateUnit.MicrodegreeCelsiusPerSecond => (MicrodegreesCelsiusPerSecondInOneDegreeCelsiusPerSecond, MicrodegreesCelsiusPerSecondTolerance), TemperatureChangeRateUnit.MillidegreeCelsiusPerSecond => (MillidegreesCelsiusPerSecondInOneDegreeCelsiusPerSecond, MillidegreesCelsiusPerSecondTolerance), TemperatureChangeRateUnit.NanodegreeCelsiusPerSecond => (NanodegreesCelsiusPerSecondInOneDegreeCelsiusPerSecond, NanodegreesCelsiusPerSecondTolerance), _ => throw new NotSupportedException() });
/// <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 TemperatureChangeRate Parse(string str, [CanBeNull] IFormatProvider provider) { if (str == null) { throw new ArgumentNullException(nameof(str)); } provider = provider ?? UnitSystem.DefaultCulture; return(QuantityParser.Parse <TemperatureChangeRate, TemperatureChangeRateUnit>(str, provider, delegate(string value, string unit, IFormatProvider formatProvider2) { double parsedValue = double.Parse(value, formatProvider2); TemperatureChangeRateUnit parsedUnit = ParseUnit(unit, formatProvider2); return From(parsedValue, parsedUnit); }, (x, y) => FromDegreesCelsiusPerSecond(x.DegreesCelsiusPerSecond + y.DegreesCelsiusPerSecond))); }
public string ToString(TemperatureChangeRateUnit 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)); }
/// <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 TemperatureChangeRate 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 <TemperatureChangeRate, TemperatureChangeRateUnit>(str, provider, delegate(string value, string unit, IFormatProvider formatProvider2) { double parsedValue = double.Parse(value, formatProvider2); TemperatureChangeRateUnit parsedUnit = ParseUnit(unit, formatProvider2); return From(parsedValue, parsedUnit); }, (x, y) => FromDegreesCelsiusPerSecond(x.DegreesCelsiusPerSecond + y.DegreesCelsiusPerSecond))); }
public string ToString(TemperatureChangeRateUnit 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)); }
// Windows Runtime Component does not support nullable types (double?): https://msdn.microsoft.com/en-us/library/br230301.aspx #if !WINDOWS_UWP /// <summary> /// Dynamically convert from value and unit enum <see cref="TemperatureChangeRateUnit" /> to <see cref="TemperatureChangeRate" />. /// </summary> /// <param name="value">Value to convert from.</param> /// <param name="fromUnit">Unit to convert from.</param> /// <returns>TemperatureChangeRate unit value.</returns> public static TemperatureChangeRate?From(double?value, TemperatureChangeRateUnit fromUnit) { if (!value.HasValue) { return(null); } switch (fromUnit) { case TemperatureChangeRateUnit.CentidegreeCelsiusPerSecond: return(FromCentidegreesCelsiusPerSecond(value.Value)); case TemperatureChangeRateUnit.DecadegreeCelsiusPerSecond: return(FromDecadegreesCelsiusPerSecond(value.Value)); case TemperatureChangeRateUnit.DecidegreeCelsiusPerSecond: return(FromDecidegreesCelsiusPerSecond(value.Value)); case TemperatureChangeRateUnit.DegreeCelsiusPerMinute: return(FromDegreesCelsiusPerMinute(value.Value)); case TemperatureChangeRateUnit.DegreeCelsiusPerSecond: return(FromDegreesCelsiusPerSecond(value.Value)); case TemperatureChangeRateUnit.HectodegreeCelsiusPerSecond: return(FromHectodegreesCelsiusPerSecond(value.Value)); case TemperatureChangeRateUnit.KilodegreeCelsiusPerSecond: return(FromKilodegreesCelsiusPerSecond(value.Value)); case TemperatureChangeRateUnit.MicrodegreeCelsiusPerSecond: return(FromMicrodegreesCelsiusPerSecond(value.Value)); case TemperatureChangeRateUnit.MillidegreeCelsiusPerSecond: return(FromMillidegreesCelsiusPerSecond(value.Value)); case TemperatureChangeRateUnit.NanodegreeCelsiusPerSecond: return(FromNanodegreesCelsiusPerSecond(value.Value)); default: throw new NotImplementedException("fromUnit: " + fromUnit); } }
/// <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 TemperatureChangeRate 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 <TemperatureChangeRate>(str, formatProvider, delegate(string value, string unit, IFormatProvider formatProvider2) { double parsedValue = double.Parse(value, formatProvider2); TemperatureChangeRateUnit parsedUnit = ParseUnit(unit, formatProvider2); return From(parsedValue, parsedUnit); }, (x, y) => FromDegreesCelsiusPerSecond(x.DegreesCelsiusPerSecond + y.DegreesCelsiusPerSecond))); }
public string ToString(TemperatureChangeRateUnit 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 TemperatureChangeRate 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 <TemperatureChangeRate, TemperatureChangeRateUnit>(str, formatProvider, delegate(string value, string unit, IFormatProvider formatProvider2) { double parsedValue = double.Parse(value, formatProvider2); TemperatureChangeRateUnit parsedUnit = ParseUnit(unit, formatProvider2); return From(parsedValue, parsedUnit); }, (x, y) => FromDegreesCelsiusPerSecond(x.DegreesCelsiusPerSecond + y.DegreesCelsiusPerSecond))); }
public string ToString(TemperatureChangeRateUnit 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)); }
/// <summary> /// Converts this TemperatureChangeRate to another TemperatureChangeRate with the unit representation <paramref name="unit" />. /// </summary> /// <returns>A TemperatureChangeRate with the specified unit.</returns> public TemperatureChangeRate ToUnit(TemperatureChangeRateUnit unit) { var convertedValue = AsBaseNumericType(unit); return(new TemperatureChangeRate(convertedValue, unit)); }
/// <summary> /// Parse a unit string. /// </summary> /// <param name="str">String to parse. Typically in the form: {number} {unit}</param> /// <param name="unit">The parsed unit if successful.</param> /// <returns>True if successful, otherwise false.</returns> /// <example> /// Length.TryParseUnit("m", new CultureInfo("en-US")); /// </example> /// <param name="cultureName">Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to <see cref="GlobalConfiguration.DefaultCulture" /> if null.</param> public static bool TryParseUnit(string str, [CanBeNull] string cultureName, out TemperatureChangeRateUnit unit) { IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); return(UnitParser.Default.TryParse <TemperatureChangeRateUnit>(str, provider, out unit)); }
public static bool TryParseUnit(string str, out TemperatureChangeRateUnit unit) { return(TryParseUnit(str, null, out unit)); }
public static TemperatureChangeRate From(decimal value, TemperatureChangeRateUnit fromUnit) { return(new TemperatureChangeRate((decimal)value, fromUnit)); }
/// <summary> /// Get unit abbreviation string. /// </summary> /// <param name="unit">Unit to get abbreviation for.</param> /// <returns>Unit abbreviation string.</returns> /// <param name="cultureName">Name of culture (ex: "en-US") to use when parsing number and unit. Defaults to <see cref="GlobalConfiguration.DefaultCulture" /> if null.</param> public static string GetAbbreviation(TemperatureChangeRateUnit unit, [CanBeNull] string cultureName) { IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); return(UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit, provider)); }
public static string GetAbbreviation(TemperatureChangeRateUnit unit, [CanBeNull] Culture culture) { return(UnitSystem.GetCached(culture).GetDefaultAbbreviation(unit)); }
/// <summary> /// Get unit abbreviation string. /// </summary> /// <param name="unit">Unit to get abbreviation for.</param> /// <returns>Unit abbreviation string.</returns> public static string GetAbbreviation(TemperatureChangeRateUnit unit) { return(GetAbbreviation(unit, null)); }
/// <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="cultureName">Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to <see cref="UnitSystem" />'s default culture.</param> /// <returns>String representation.</returns> public string ToString(TemperatureChangeRateUnit unit, [CanBeNull] string cultureName) { return(ToString(unit, cultureName, 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(TemperatureChangeRateUnit unit) { return(ToString(unit, null, 2)); }