diff --git a/UnitsNet.Tests/CustomCode/TemperatureTests.cs b/UnitsNet.Tests/CustomCode/TemperatureTests.cs index ad2c029b78..0aa5f9d234 100644 --- a/UnitsNet.Tests/CustomCode/TemperatureTests.cs +++ b/UnitsNet.Tests/CustomCode/TemperatureTests.cs @@ -61,7 +61,7 @@ public void DividedByTemperatureDeltaEqualsTemperature(TemperatureUnit unit, int // Act Temperature resultTemp = temperature.Divide(divisor, unit); - string actual = resultTemp.ToString(unit, CultureInfo.InvariantCulture, "{0:0} {1}"); + string actual = resultTemp.ToUnit(unit).ToString(CultureInfo.InvariantCulture, "{0:0} {1}"); Assert.Equal(expected, actual); } @@ -81,7 +81,7 @@ public void MultiplyByTemperatureDeltaEqualsTemperature(TemperatureUnit unit, in // Act Temperature resultTemp = temperature.Multiply(factor, unit); - string actual = resultTemp.ToString(unit, CultureInfo.InvariantCulture, "{0:0} {1}"); + string actual = resultTemp.ToUnit(unit).ToString(CultureInfo.InvariantCulture, "{0:0} {1}"); Assert.Equal(expected, actual); } @@ -100,7 +100,7 @@ public void TemperatureDeltaPlusTemperatureEqualsTemperature(TemperatureUnit uni // Act Temperature resultTemp = delta + temperature; - string actual = resultTemp.ToString(unit, CultureInfo.InvariantCulture, "{0:0} {1}"); + string actual = resultTemp.ToUnit(unit).ToString(CultureInfo.InvariantCulture, "{0:0} {1}"); Assert.Equal(expected, actual); } @@ -119,7 +119,7 @@ public void TemperatureMinusTemperatureDeltaEqualsTemperature(TemperatureUnit un // Act Temperature resultTemp = temperature - delta; - string actual = resultTemp.ToString(unit, CultureInfo.InvariantCulture, "{0:0} {1}"); + string actual = resultTemp.ToUnit(unit).ToString(CultureInfo.InvariantCulture, "{0:0} {1}"); Assert.Equal(expected, actual); } @@ -138,8 +138,8 @@ public void TemperaturePlusTemperatureDeltaEqualsTemperature(TemperatureUnit uni // Act Temperature resultTemp = temperature + delta; - string actual = resultTemp.ToString(unit, CultureInfo.InvariantCulture, "{0:0} {1}"); + string actual = resultTemp.ToUnit(unit).ToString(CultureInfo.InvariantCulture, "{0:0} {1}"); Assert.Equal(expected, actual); } } -} \ No newline at end of file +} diff --git a/UnitsNet.Tests/QuantityTests.ToString.cs b/UnitsNet.Tests/QuantityTests.ToString.cs index 970fccb3a6..0c6c21e935 100644 --- a/UnitsNet.Tests/QuantityTests.ToString.cs +++ b/UnitsNet.Tests/QuantityTests.ToString.cs @@ -106,10 +106,10 @@ public void ConvertsToTheGivenUnit() try { GlobalConfiguration.DefaultCulture = CultureInfo.InvariantCulture; - Assert.Equal("5,000 g", Mass.FromKilograms(5).ToString(MassUnit.Gram)); - Assert.Equal("5 kg", Mass.FromGrams(5000).ToString(MassUnit.Kilogram)); - Assert.Equal("0.05 m", Length.FromCentimeters(5).ToString(LengthUnit.Meter)); - Assert.Equal("1.97 in", Length.FromCentimeters(5).ToString(LengthUnit.Inch)); + Assert.Equal("5,000 g", Mass.FromKilograms(5).ToUnit(MassUnit.Gram).ToString()); + Assert.Equal("5 kg", Mass.FromGrams(5000).ToUnit(MassUnit.Kilogram).ToString()); + Assert.Equal("0.05 m", Length.FromCentimeters(5).ToUnit(LengthUnit.Meter).ToString()); + Assert.Equal("1.97 in", Length.FromCentimeters(5).ToUnit(LengthUnit.Inch).ToString()); } finally { @@ -124,9 +124,9 @@ public void FormatsNumberUsingGivenCulture() try { GlobalConfiguration.DefaultCulture = CultureInfo.InvariantCulture; - Assert.Equal("0.05 m", Length.FromCentimeters(5).ToString(LengthUnit.Meter, null)); - Assert.Equal("0.05 m", Length.FromCentimeters(5).ToString(LengthUnit.Meter, CultureInfo.InvariantCulture)); - Assert.Equal("0,05 m", Length.FromCentimeters(5).ToString(LengthUnit.Meter, new CultureInfo("nb-NO"))); + Assert.Equal("0.05 m", Length.FromCentimeters(5).ToUnit(LengthUnit.Meter).ToString(null)); + Assert.Equal("0.05 m", Length.FromCentimeters(5).ToUnit(LengthUnit.Meter).ToString(CultureInfo.InvariantCulture)); + Assert.Equal("0,05 m", Length.FromCentimeters(5).ToUnit(LengthUnit.Meter).ToString(new CultureInfo("nb-NO"))); } finally { @@ -141,9 +141,9 @@ public void FormatsNumberUsingGivenDigitsAfterRadix() try { GlobalConfiguration.DefaultCulture = CultureInfo.InvariantCulture; - Assert.Equal("0.05 m", Length.FromCentimeters(5).ToString(LengthUnit.Meter, null, 4)); - Assert.Equal("1.97 in", Length.FromCentimeters(5).ToString(LengthUnit.Inch, null, 2)); - Assert.Equal("1.9685 in", Length.FromCentimeters(5).ToString(LengthUnit.Inch, null, 4)); + Assert.Equal("0.05 m", Length.FromCentimeters(5).ToUnit(LengthUnit.Meter).ToString(null, 4)); + Assert.Equal("1.97 in", Length.FromCentimeters(5).ToUnit(LengthUnit.Inch).ToString(null, 2)); + Assert.Equal("1.9685 in", Length.FromCentimeters(5).ToUnit(LengthUnit.Inch).ToString(null, 4)); } finally { diff --git a/UnitsNet.Tests/UnitAbbreviationsCacheTests.cs b/UnitsNet.Tests/UnitAbbreviationsCacheTests.cs index 449c5cf042..cfdf98338f 100644 --- a/UnitsNet.Tests/UnitAbbreviationsCacheTests.cs +++ b/UnitsNet.Tests/UnitAbbreviationsCacheTests.cs @@ -55,7 +55,7 @@ public UnitAbbreviationsCacheTests(ITestOutputHelper output) [InlineData(0.115, "0.12 m")] public void DefaultToStringFormatting(double value, string expected) { - string actual = Length.FromMeters(value).ToString(LengthUnit.Meter, AmericanCulture); + string actual = Length.FromMeters(value).ToUnit(LengthUnit.Meter).ToString(AmericanCulture); Assert.Equal(expected, actual); } @@ -77,7 +77,7 @@ private enum CustomUnit [InlineData("it-IT")] public void CommaRadixPointCultureFormatting(string culture) { - Assert.Equal("0,12 m", Length.FromMeters(0.12).ToString(LengthUnit.Meter, GetCulture(culture))); + Assert.Equal("0,12 m", Length.FromMeters(0.12).ToUnit(LengthUnit.Meter).ToString(GetCulture(culture))); } // These cultures all use a decimal point for the radix point @@ -89,7 +89,7 @@ public void CommaRadixPointCultureFormatting(string culture) [InlineData("es-MX")] public void DecimalRadixPointCultureFormatting(string culture) { - Assert.Equal("0.12 m", Length.FromMeters(0.12).ToString(LengthUnit.Meter, GetCulture(culture))); + Assert.Equal("0.12 m", Length.FromMeters(0.12).ToUnit(LengthUnit.Meter).ToString(GetCulture(culture))); } // These cultures all use a comma in digit grouping @@ -102,7 +102,7 @@ public void DecimalRadixPointCultureFormatting(string culture) public void CommaDigitGroupingCultureFormatting(string cultureName) { CultureInfo culture = GetCulture(cultureName); - Assert.Equal("1,111 m", Length.FromMeters(1111).ToString(LengthUnit.Meter, culture)); + Assert.Equal("1,111 m", Length.FromMeters(1111).ToUnit(LengthUnit.Meter).ToString(culture)); // Feet/Inch and Stone/Pound combinations are only used (customarily) in the US, UK and maybe Ireland - all English speaking countries. // FeetInches returns a whole number of feet, with the remainder expressed (rounded) in inches. Same for SonePounds. @@ -119,7 +119,7 @@ public void CommaDigitGroupingCultureFormatting(string cultureName) public void SpaceDigitGroupingCultureFormatting(string culture) { // Note: the space used in digit groupings is actually a "thin space" Unicode character U+2009 - Assert.Equal("1 111 m", Length.FromMeters(1111).ToString(LengthUnit.Meter, GetCulture(culture))); + Assert.Equal("1 111 m", Length.FromMeters(1111).ToUnit(LengthUnit.Meter).ToString(GetCulture(culture))); } // These cultures all use a decimal point in digit grouping @@ -131,7 +131,7 @@ public void SpaceDigitGroupingCultureFormatting(string culture) [InlineData("it-IT")] public void DecimalPointDigitGroupingCultureFormatting(string culture) { - Assert.Equal("1.111 m", Length.FromMeters(1111).ToString(LengthUnit.Meter, GetCulture(culture))); + Assert.Equal("1.111 m", Length.FromMeters(1111).ToUnit(LengthUnit.Meter).ToString(GetCulture(culture))); } [Theory] @@ -143,7 +143,7 @@ public void DecimalPointDigitGroupingCultureFormatting(string culture) [InlineData(6, "1.123457 m")] public void CustomNumberOfSignificantDigitsAfterRadixFormatting(int significantDigitsAfterRadix, string expected) { - string actual = Length.FromMeters(1.123456789).ToString(LengthUnit.Meter, AmericanCulture, significantDigitsAfterRadix); + string actual = Length.FromMeters(1.123456789).ToUnit(LengthUnit.Meter).ToString(AmericanCulture, significantDigitsAfterRadix); Assert.Equal(expected, actual); } @@ -158,7 +158,7 @@ public void CustomNumberOfSignificantDigitsAfterRadixFormatting(int significantD public void RoundingErrorsWithSignificantDigitsAfterRadixFormatting(double value, int maxSignificantDigitsAfterRadix, string expected) { - string actual = Length.FromMeters(value).ToString(LengthUnit.Meter, AmericanCulture, maxSignificantDigitsAfterRadix); + string actual = Length.FromMeters(value).ToUnit(LengthUnit.Meter).ToString(AmericanCulture, maxSignificantDigitsAfterRadix); Assert.Equal(expected, actual); } @@ -170,7 +170,7 @@ public void RoundingErrorsWithSignificantDigitsAfterRadixFormatting(double value [InlineData(1.99e-4, "1.99e-04 m")] public void ScientificNotationLowerInterval(double value, string expected) { - string actual = Length.FromMeters(value).ToString(LengthUnit.Meter, AmericanCulture); + string actual = Length.FromMeters(value).ToUnit(LengthUnit.Meter).ToString(AmericanCulture); Assert.Equal(expected, actual); } @@ -181,7 +181,7 @@ public void ScientificNotationLowerInterval(double value, string expected) [InlineData(999.99, "999.99 m")] public void FixedPointNotationIntervalFormatting(double value, string expected) { - string actual = Length.FromMeters(value).ToString(LengthUnit.Meter, AmericanCulture); + string actual = Length.FromMeters(value).ToUnit(LengthUnit.Meter).ToString(AmericanCulture); Assert.Equal(expected, actual); } @@ -193,7 +193,7 @@ public void FixedPointNotationIntervalFormatting(double value, string expected) [InlineData(999999.99, "999,999.99 m")] public void FixedPointNotationWithDigitGroupingIntervalFormatting(double value, string expected) { - string actual = Length.FromMeters(value).ToString(LengthUnit.Meter, AmericanCulture); + string actual = Length.FromMeters(value).ToUnit(LengthUnit.Meter).ToString(AmericanCulture); Assert.Equal(expected, actual); } @@ -204,7 +204,7 @@ public void FixedPointNotationWithDigitGroupingIntervalFormatting(double value, [InlineData(double.MaxValue, "1.8e+308 m")] public void ScientificNotationUpperIntervalFormatting(double value, string expected) { - string actual = Length.FromMeters(value).ToString(LengthUnit.Meter, AmericanCulture); + string actual = Length.FromMeters(value).ToUnit(LengthUnit.Meter).ToString(AmericanCulture); Assert.Equal(expected, actual); } @@ -231,35 +231,35 @@ public void AllUnitsImplementToStringForInvariantCulture() [Fact] public void ToString_WithNorwegianCulture() { - Assert.Equal("1 °", Angle.FromDegrees(1).ToString(AngleUnit.Degree, NorwegianCulture)); - Assert.Equal("1 m²", Area.FromSquareMeters(1).ToString(AreaUnit.SquareMeter, NorwegianCulture)); - Assert.Equal("1 V", ElectricPotential.FromVolts(1).ToString(ElectricPotentialUnit.Volt, NorwegianCulture)); - Assert.Equal("1 m³/s", VolumeFlow.FromCubicMetersPerSecond(1).ToString(VolumeFlowUnit.CubicMeterPerSecond, NorwegianCulture)); - Assert.Equal("1 N", Force.FromNewtons(1).ToString(ForceUnit.Newton, NorwegianCulture)); - Assert.Equal("1 m", Length.FromMeters(1).ToString(LengthUnit.Meter, NorwegianCulture)); - Assert.Equal("1 kg", Mass.FromKilograms(1).ToString(MassUnit.Kilogram, NorwegianCulture)); - Assert.Equal("1 Pa", Pressure.FromPascals(1).ToString(PressureUnit.Pascal, NorwegianCulture)); - Assert.Equal("1 rad/s", RotationalSpeed.FromRadiansPerSecond(1).ToString(RotationalSpeedUnit.RadianPerSecond, NorwegianCulture)); - Assert.Equal("1 K", Temperature.FromKelvins(1).ToString(TemperatureUnit.Kelvin, NorwegianCulture)); - Assert.Equal("1 N·m", Torque.FromNewtonMeters(1).ToString(TorqueUnit.NewtonMeter, NorwegianCulture)); - Assert.Equal("1 m³", Volume.FromCubicMeters(1).ToString(VolumeUnit.CubicMeter, NorwegianCulture)); + Assert.Equal("1 °", Angle.FromDegrees(1).ToUnit(AngleUnit.Degree).ToString(NorwegianCulture)); + Assert.Equal("1 m²", Area.FromSquareMeters(1).ToUnit(AreaUnit.SquareMeter).ToString(NorwegianCulture)); + Assert.Equal("1 V", ElectricPotential.FromVolts(1).ToUnit(ElectricPotentialUnit.Volt).ToString(NorwegianCulture)); + Assert.Equal("1 m³/s", VolumeFlow.FromCubicMetersPerSecond(1).ToUnit(VolumeFlowUnit.CubicMeterPerSecond).ToString(NorwegianCulture)); + Assert.Equal("1 N", Force.FromNewtons(1).ToUnit(ForceUnit.Newton).ToString(NorwegianCulture)); + Assert.Equal("1 m", Length.FromMeters(1).ToUnit(LengthUnit.Meter).ToString(NorwegianCulture)); + Assert.Equal("1 kg", Mass.FromKilograms(1).ToUnit(MassUnit.Kilogram).ToString(NorwegianCulture)); + Assert.Equal("1 Pa", Pressure.FromPascals(1).ToUnit(PressureUnit.Pascal).ToString(NorwegianCulture)); + Assert.Equal("1 rad/s", RotationalSpeed.FromRadiansPerSecond(1).ToUnit(RotationalSpeedUnit.RadianPerSecond).ToString(NorwegianCulture)); + Assert.Equal("1 K", Temperature.FromKelvins(1).ToUnit(TemperatureUnit.Kelvin).ToString(NorwegianCulture)); + Assert.Equal("1 N·m", Torque.FromNewtonMeters(1).ToUnit(TorqueUnit.NewtonMeter).ToString(NorwegianCulture)); + Assert.Equal("1 m³", Volume.FromCubicMeters(1).ToUnit(VolumeUnit.CubicMeter).ToString(NorwegianCulture)); } [Fact] public void ToString_WithRussianCulture() { - Assert.Equal("1 °", Angle.FromDegrees(1).ToString(AngleUnit.Degree, RussianCulture)); - Assert.Equal("1 м²", Area.FromSquareMeters(1).ToString(AreaUnit.SquareMeter, RussianCulture)); - Assert.Equal("1 В", ElectricPotential.FromVolts(1).ToString(ElectricPotentialUnit.Volt, RussianCulture)); - Assert.Equal("1 м³/с", VolumeFlow.FromCubicMetersPerSecond(1).ToString(VolumeFlowUnit.CubicMeterPerSecond, RussianCulture)); - Assert.Equal("1 Н", Force.FromNewtons(1).ToString(ForceUnit.Newton, RussianCulture)); - Assert.Equal("1 м", Length.FromMeters(1).ToString(LengthUnit.Meter, RussianCulture)); - Assert.Equal("1 кг", Mass.FromKilograms(1).ToString(MassUnit.Kilogram, RussianCulture)); - Assert.Equal("1 Па", Pressure.FromPascals(1).ToString(PressureUnit.Pascal, RussianCulture)); - Assert.Equal("1 рад/с", RotationalSpeed.FromRadiansPerSecond(1).ToString(RotationalSpeedUnit.RadianPerSecond, RussianCulture)); - Assert.Equal("1 K", Temperature.FromKelvins(1).ToString(TemperatureUnit.Kelvin, RussianCulture)); - Assert.Equal("1 Н·м", Torque.FromNewtonMeters(1).ToString(TorqueUnit.NewtonMeter, RussianCulture)); - Assert.Equal("1 м³", Volume.FromCubicMeters(1).ToString(VolumeUnit.CubicMeter, RussianCulture)); + Assert.Equal("1 °", Angle.FromDegrees(1).ToUnit(AngleUnit.Degree).ToString(RussianCulture)); + Assert.Equal("1 м²", Area.FromSquareMeters(1).ToUnit(AreaUnit.SquareMeter).ToString(RussianCulture)); + Assert.Equal("1 В", ElectricPotential.FromVolts(1).ToUnit(ElectricPotentialUnit.Volt).ToString(RussianCulture)); + Assert.Equal("1 м³/с", VolumeFlow.FromCubicMetersPerSecond(1).ToUnit(VolumeFlowUnit.CubicMeterPerSecond).ToString(RussianCulture)); + Assert.Equal("1 Н", Force.FromNewtons(1).ToUnit(ForceUnit.Newton).ToString(RussianCulture)); + Assert.Equal("1 м", Length.FromMeters(1).ToUnit(LengthUnit.Meter).ToString(RussianCulture)); + Assert.Equal("1 кг", Mass.FromKilograms(1).ToUnit(MassUnit.Kilogram).ToString(RussianCulture)); + Assert.Equal("1 Па", Pressure.FromPascals(1).ToUnit(PressureUnit.Pascal).ToString(RussianCulture)); + Assert.Equal("1 рад/с", RotationalSpeed.FromRadiansPerSecond(1).ToUnit(RotationalSpeedUnit.RadianPerSecond).ToString(RussianCulture)); + Assert.Equal("1 K", Temperature.FromKelvins(1).ToUnit(TemperatureUnit.Kelvin).ToString(RussianCulture)); + Assert.Equal("1 Н·м", Torque.FromNewtonMeters(1).ToUnit(TorqueUnit.NewtonMeter).ToString(RussianCulture)); + Assert.Equal("1 м³", Volume.FromCubicMeters(1).ToUnit(VolumeUnit.CubicMeter).ToString(RussianCulture)); } [Fact] diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Information.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Information.WindowsRuntimeComponent.g.cs index be689fa22b..0e5491bac3 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Information.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Information.WindowsRuntimeComponent.g.cs @@ -937,64 +937,51 @@ private decimal AsBaseNumericType(InformationUnit unit) /// String representation. public override string ToString() { - return ToString(Unit); - } - - /// - /// Get string representation of value and unit. Using current UI culture and two significant digits after radix. - /// - /// Unit representation to use. - /// String representation. - public string ToString(InformationUnit unit) - { - return ToString(unit, null, 2); + return ToString(null); } /// /// Get string representation of value and unit. Using two significant digits after radix. /// - /// Unit representation to use. /// String representation. /// Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to if null. - public string ToString(InformationUnit unit, [CanBeNull] string cultureName) + public string ToString([CanBeNull] string cultureName) { var provider = cultureName; - return ToString(unit, provider, 2); + return ToString(provider, 2); } /// /// Get string representation of value and unit. /// - /// Unit representation to use. /// The number of significant digits after the radix point. /// String representation. /// Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to if null. - public string ToString(InformationUnit unit, [CanBeNull] string cultureName, int significantDigitsAfterRadix) + public string ToString(string cultureName, int significantDigitsAfterRadix) { var provider = cultureName; - double value = As(unit); - string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix); - return ToString(unit, provider, format); + var value = Convert.ToDouble(Value); + var format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix); + return ToString(provider, format); } /// /// Get string representation of value and unit. /// - /// Unit representation to use. /// String format to use. Default: "{0:0.##} {1} for value and unit abbreviation respectively." /// Arguments for string format. Value and unit are implictly included as arguments 0 and 1. /// String representation. /// Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to if null. - public string ToString(InformationUnit unit, [CanBeNull] string cultureName, [NotNull] string format, [NotNull] params object[] args) + public string ToString([CanBeNull] string cultureName, [NotNull] string format, [NotNull] params object[] args) { - IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); + var provider = GetFormatProviderFromCultureName(cultureName); if (format == null) throw new ArgumentNullException(nameof(format)); if (args == null) throw new ArgumentNullException(nameof(args)); provider = provider ?? GlobalConfiguration.DefaultCulture; - double value = As(unit); - object[] formatArgs = UnitFormatter.GetFormatArgs(unit, value, provider, args); + var value = Convert.ToDouble(Value); + var formatArgs = UnitFormatter.GetFormatArgs(Unit, value, provider, args); return string.Format(provider, format, formatArgs); } diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Length.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Length.WindowsRuntimeComponent.g.cs index 85e037b5d1..ff1d2450fb 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Length.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Length.WindowsRuntimeComponent.g.cs @@ -869,64 +869,51 @@ private double AsBaseNumericType(LengthUnit unit) /// String representation. public override string ToString() { - return ToString(Unit); - } - - /// - /// Get string representation of value and unit. Using current UI culture and two significant digits after radix. - /// - /// Unit representation to use. - /// String representation. - public string ToString(LengthUnit unit) - { - return ToString(unit, null, 2); + return ToString(null); } /// /// Get string representation of value and unit. Using two significant digits after radix. /// - /// Unit representation to use. /// String representation. /// Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to if null. - public string ToString(LengthUnit unit, [CanBeNull] string cultureName) + public string ToString([CanBeNull] string cultureName) { var provider = cultureName; - return ToString(unit, provider, 2); + return ToString(provider, 2); } /// /// Get string representation of value and unit. /// - /// Unit representation to use. /// The number of significant digits after the radix point. /// String representation. /// Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to if null. - public string ToString(LengthUnit unit, [CanBeNull] string cultureName, int significantDigitsAfterRadix) + public string ToString(string cultureName, int significantDigitsAfterRadix) { var provider = cultureName; - double value = As(unit); - string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix); - return ToString(unit, provider, format); + var value = Convert.ToDouble(Value); + var format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix); + return ToString(provider, format); } /// /// Get string representation of value and unit. /// - /// Unit representation to use. /// String format to use. Default: "{0:0.##} {1} for value and unit abbreviation respectively." /// Arguments for string format. Value and unit are implictly included as arguments 0 and 1. /// String representation. /// Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to if null. - public string ToString(LengthUnit unit, [CanBeNull] string cultureName, [NotNull] string format, [NotNull] params object[] args) + public string ToString([CanBeNull] string cultureName, [NotNull] string format, [NotNull] params object[] args) { - IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); + var provider = GetFormatProviderFromCultureName(cultureName); if (format == null) throw new ArgumentNullException(nameof(format)); if (args == null) throw new ArgumentNullException(nameof(args)); provider = provider ?? GlobalConfiguration.DefaultCulture; - double value = As(unit); - object[] formatArgs = UnitFormatter.GetFormatArgs(unit, value, provider, args); + var value = Convert.ToDouble(Value); + var formatArgs = UnitFormatter.GetFormatArgs(Unit, value, provider, args); return string.Format(provider, format, formatArgs); } diff --git a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Level.WindowsRuntimeComponent.g.cs b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Level.WindowsRuntimeComponent.g.cs index 891c98f5a1..1104c74058 100644 --- a/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Level.WindowsRuntimeComponent.g.cs +++ b/UnitsNet.WindowsRuntimeComponent/GeneratedCode/Quantities/Level.WindowsRuntimeComponent.g.cs @@ -529,64 +529,51 @@ private double AsBaseNumericType(LevelUnit unit) /// String representation. public override string ToString() { - return ToString(Unit); - } - - /// - /// Get string representation of value and unit. Using current UI culture and two significant digits after radix. - /// - /// Unit representation to use. - /// String representation. - public string ToString(LevelUnit unit) - { - return ToString(unit, null, 2); + return ToString(null); } /// /// Get string representation of value and unit. Using two significant digits after radix. /// - /// Unit representation to use. /// String representation. /// Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to if null. - public string ToString(LevelUnit unit, [CanBeNull] string cultureName) + public string ToString([CanBeNull] string cultureName) { var provider = cultureName; - return ToString(unit, provider, 2); + return ToString(provider, 2); } /// /// Get string representation of value and unit. /// - /// Unit representation to use. /// The number of significant digits after the radix point. /// String representation. /// Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to if null. - public string ToString(LevelUnit unit, [CanBeNull] string cultureName, int significantDigitsAfterRadix) + public string ToString(string cultureName, int significantDigitsAfterRadix) { var provider = cultureName; - double value = As(unit); - string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix); - return ToString(unit, provider, format); + var value = Convert.ToDouble(Value); + var format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix); + return ToString(provider, format); } /// /// Get string representation of value and unit. /// - /// Unit representation to use. /// String format to use. Default: "{0:0.##} {1} for value and unit abbreviation respectively." /// Arguments for string format. Value and unit are implictly included as arguments 0 and 1. /// String representation. /// Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to if null. - public string ToString(LevelUnit unit, [CanBeNull] string cultureName, [NotNull] string format, [NotNull] params object[] args) + public string ToString([CanBeNull] string cultureName, [NotNull] string format, [NotNull] params object[] args) { - IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); + var provider = GetFormatProviderFromCultureName(cultureName); if (format == null) throw new ArgumentNullException(nameof(format)); if (args == null) throw new ArgumentNullException(nameof(args)); provider = provider ?? GlobalConfiguration.DefaultCulture; - double value = As(unit); - object[] formatArgs = UnitFormatter.GetFormatArgs(unit, value, provider, args); + var value = Convert.ToDouble(Value); + var formatArgs = UnitFormatter.GetFormatArgs(Unit, value, provider, args); return string.Format(provider, format, formatArgs); } diff --git a/UnitsNet/GeneratedCode/Quantities/Information.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Information.NetFramework.g.cs index 58365f60fb..c12969f03e 100644 --- a/UnitsNet/GeneratedCode/Quantities/Information.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Information.NetFramework.g.cs @@ -949,61 +949,48 @@ private decimal AsBaseNumericType(InformationUnit unit) /// String representation. public override string ToString() { - return ToString(Unit); - } - - /// - /// Get string representation of value and unit. Using current UI culture and two significant digits after radix. - /// - /// Unit representation to use. - /// String representation. - public string ToString(InformationUnit unit) - { - return ToString(unit, null, 2); + return ToString(null); } /// /// Get string representation of value and unit. Using two significant digits after radix. /// - /// Unit representation to use. /// String representation. /// Format to use for localization and number formatting. Defaults to if null. - public string ToString(InformationUnit unit, [CanBeNull] IFormatProvider provider) + public string ToString([CanBeNull] IFormatProvider provider) { - return ToString(unit, provider, 2); + return ToString(provider, 2); } /// /// Get string representation of value and unit. /// - /// Unit representation to use. /// The number of significant digits after the radix point. /// String representation. /// Format to use for localization and number formatting. Defaults to if null. - public string ToString(InformationUnit unit, [CanBeNull] IFormatProvider provider, int significantDigitsAfterRadix) + public string ToString([CanBeNull] IFormatProvider provider, int significantDigitsAfterRadix) { - double value = As(unit); - string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix); - return ToString(unit, provider, format); + var value = Convert.ToDouble(Value); + var format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix); + return ToString(provider, format); } /// /// Get string representation of value and unit. /// - /// Unit representation to use. /// String format to use. Default: "{0:0.##} {1} for value and unit abbreviation respectively." /// Arguments for string format. Value and unit are implictly included as arguments 0 and 1. /// String representation. /// Format to use for localization and number formatting. Defaults to if null. - public string ToString(InformationUnit unit, [CanBeNull] IFormatProvider provider, [NotNull] string format, [NotNull] params object[] args) + public string ToString([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 ?? GlobalConfiguration.DefaultCulture; - double value = As(unit); - object[] formatArgs = UnitFormatter.GetFormatArgs(unit, value, provider, args); + var value = Convert.ToDouble(Value); + var formatArgs = UnitFormatter.GetFormatArgs(Unit, value, provider, args); return string.Format(provider, format, formatArgs); } diff --git a/UnitsNet/GeneratedCode/Quantities/Length.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Length.NetFramework.g.cs index c1deaa2b60..2b75619d57 100644 --- a/UnitsNet/GeneratedCode/Quantities/Length.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Length.NetFramework.g.cs @@ -885,61 +885,48 @@ private double AsBaseNumericType(LengthUnit unit) /// String representation. public override string ToString() { - return ToString(Unit); - } - - /// - /// Get string representation of value and unit. Using current UI culture and two significant digits after radix. - /// - /// Unit representation to use. - /// String representation. - public string ToString(LengthUnit unit) - { - return ToString(unit, null, 2); + return ToString(null); } /// /// Get string representation of value and unit. Using two significant digits after radix. /// - /// Unit representation to use. /// String representation. /// Format to use for localization and number formatting. Defaults to if null. - public string ToString(LengthUnit unit, [CanBeNull] IFormatProvider provider) + public string ToString([CanBeNull] IFormatProvider provider) { - return ToString(unit, provider, 2); + return ToString(provider, 2); } /// /// Get string representation of value and unit. /// - /// Unit representation to use. /// The number of significant digits after the radix point. /// String representation. /// Format to use for localization and number formatting. Defaults to if null. - public string ToString(LengthUnit unit, [CanBeNull] IFormatProvider provider, int significantDigitsAfterRadix) + public string ToString([CanBeNull] IFormatProvider provider, int significantDigitsAfterRadix) { - double value = As(unit); - string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix); - return ToString(unit, provider, format); + var value = Convert.ToDouble(Value); + var format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix); + return ToString(provider, format); } /// /// Get string representation of value and unit. /// - /// Unit representation to use. /// String format to use. Default: "{0:0.##} {1} for value and unit abbreviation respectively." /// Arguments for string format. Value and unit are implictly included as arguments 0 and 1. /// String representation. /// Format to use for localization and number formatting. Defaults to if null. - public string ToString(LengthUnit unit, [CanBeNull] IFormatProvider provider, [NotNull] string format, [NotNull] params object[] args) + public string ToString([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 ?? GlobalConfiguration.DefaultCulture; - double value = As(unit); - object[] formatArgs = UnitFormatter.GetFormatArgs(unit, value, provider, args); + var value = Convert.ToDouble(Value); + var formatArgs = UnitFormatter.GetFormatArgs(Unit, value, provider, args); return string.Format(provider, format, formatArgs); } diff --git a/UnitsNet/GeneratedCode/Quantities/Level.NetFramework.g.cs b/UnitsNet/GeneratedCode/Quantities/Level.NetFramework.g.cs index 8a8e7ddae3..bf30df8c73 100644 --- a/UnitsNet/GeneratedCode/Quantities/Level.NetFramework.g.cs +++ b/UnitsNet/GeneratedCode/Quantities/Level.NetFramework.g.cs @@ -573,61 +573,48 @@ private double AsBaseNumericType(LevelUnit unit) /// String representation. public override string ToString() { - return ToString(Unit); - } - - /// - /// Get string representation of value and unit. Using current UI culture and two significant digits after radix. - /// - /// Unit representation to use. - /// String representation. - public string ToString(LevelUnit unit) - { - return ToString(unit, null, 2); + return ToString(null); } /// /// Get string representation of value and unit. Using two significant digits after radix. /// - /// Unit representation to use. /// String representation. /// Format to use for localization and number formatting. Defaults to if null. - public string ToString(LevelUnit unit, [CanBeNull] IFormatProvider provider) + public string ToString([CanBeNull] IFormatProvider provider) { - return ToString(unit, provider, 2); + return ToString(provider, 2); } /// /// Get string representation of value and unit. /// - /// Unit representation to use. /// The number of significant digits after the radix point. /// String representation. /// Format to use for localization and number formatting. Defaults to if null. - public string ToString(LevelUnit unit, [CanBeNull] IFormatProvider provider, int significantDigitsAfterRadix) + public string ToString([CanBeNull] IFormatProvider provider, int significantDigitsAfterRadix) { - double value = As(unit); - string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix); - return ToString(unit, provider, format); + var value = Convert.ToDouble(Value); + var format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix); + return ToString(provider, format); } /// /// Get string representation of value and unit. /// - /// Unit representation to use. /// String format to use. Default: "{0:0.##} {1} for value and unit abbreviation respectively." /// Arguments for string format. Value and unit are implictly included as arguments 0 and 1. /// String representation. /// Format to use for localization and number formatting. Defaults to if null. - public string ToString(LevelUnit unit, [CanBeNull] IFormatProvider provider, [NotNull] string format, [NotNull] params object[] args) + public string ToString([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 ?? GlobalConfiguration.DefaultCulture; - double value = As(unit); - object[] formatArgs = UnitFormatter.GetFormatArgs(unit, value, provider, args); + var value = Convert.ToDouble(Value); + var formatArgs = UnitFormatter.GetFormatArgs(Unit, value, provider, args); return string.Format(provider, format, formatArgs); } diff --git a/UnitsNet/IQuantity.cs b/UnitsNet/IQuantity.cs index 611d440ff9..2b499f4507 100644 --- a/UnitsNet/IQuantity.cs +++ b/UnitsNet/IQuantity.cs @@ -27,7 +27,7 @@ namespace UnitsNet /// /// Represents a quantity. /// - public interface IQuantity + public partial interface IQuantity { /// /// The of this quantity. @@ -48,47 +48,73 @@ BaseDimensions Dimensions #if !WINDOWS_UWP - public interface IQuantity : IQuantity where UnitType : Enum + public partial interface IQuantity { /// - /// Convert to the unit representation . + /// Get string representation of value and unit. Using two significant digits after radix. /// - /// Value converted to the specified unit. - double As(UnitType unit); + /// String representation. + /// Format to use for localization and number formatting. Defaults to if null. + string ToString([CanBeNull] IFormatProvider provider); /// - /// Get string representation of value and unit. Using current UI culture and two significant digits after radix. + /// Get string representation of value and unit. /// - /// Unit representation to use. + /// The number of significant digits after the radix point. /// String representation. - string ToString(UnitType unit); + /// Format to use for localization and number formatting. Defaults to if null. + string ToString([CanBeNull] IFormatProvider provider, int significantDigitsAfterRadix); /// - /// Get string representation of value and unit. Using two significant digits after radix. + /// Get string representation of value and unit. /// - /// Unit representation to use. + /// String format to use. Default: "{0:0.##} {1} for value and unit abbreviation respectively." + /// Arguments for string format. Value and unit are implictly included as arguments 0 and 1. /// String representation. /// Format to use for localization and number formatting. Defaults to if null. - string ToString(UnitType unit, [CanBeNull] IFormatProvider provider); + string ToString([CanBeNull] IFormatProvider provider, [NotNull] string format, [NotNull] params object[] args); + } + +#else + + public partial interface IQuantity + { + /// + /// Get string representation of value and unit. Using two significant digits after radix. + /// + /// String representation. + /// Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to if null. + string ToString([CanBeNull] string cultureName); /// /// Get string representation of value and unit. /// - /// Unit representation to use. /// The number of significant digits after the radix point. /// String representation. - /// Format to use for localization and number formatting. Defaults to if null. - string ToString(UnitType unit, [CanBeNull] IFormatProvider provider, int significantDigitsAfterRadix); + /// Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to if null. + string ToString(string cultureName, int significantDigitsAfterRadix); /// /// Get string representation of value and unit. /// - /// Unit representation to use. /// String format to use. Default: "{0:0.##} {1} for value and unit abbreviation respectively." /// Arguments for string format. Value and unit are implictly included as arguments 0 and 1. /// String representation. - /// Format to use for localization and number formatting. Defaults to if null. - string ToString(UnitType unit, [CanBeNull] IFormatProvider provider, [NotNull] string format, [NotNull] params object[] args); + /// Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to if null. + string ToString([CanBeNull] string cultureName, [NotNull] string format, [NotNull] params object[] args); + } + +#endif + +#if !WINDOWS_UWP + + public interface IQuantity : IQuantity where UnitType : Enum + { + /// + /// Convert to the unit representation . + /// + /// Value converted to the specified unit. + double As(UnitType unit); /// /// The unit this quantity was constructed with or the BaseUnit if the default constructor was used. diff --git a/UnitsNet/Scripts/Include-GenerateQuantitySourceCodeNetFramework.ps1 b/UnitsNet/Scripts/Include-GenerateQuantitySourceCodeNetFramework.ps1 index 0f2b8a5d9b..a9a6aa0b73 100644 --- a/UnitsNet/Scripts/Include-GenerateQuantitySourceCodeNetFramework.ps1 +++ b/UnitsNet/Scripts/Include-GenerateQuantitySourceCodeNetFramework.ps1 @@ -178,76 +178,63 @@ if ($obsoleteAttribute) /// String representation. public override string ToString() { - return ToString(Unit); - } - - /// - /// Get string representation of value and unit. Using current UI culture and two significant digits after radix. - /// - /// Unit representation to use. - /// String representation. - public string ToString($unitEnumName unit) - { - return ToString(unit, null, 2); + return ToString(null); } /// /// Get string representation of value and unit. Using two significant digits after radix. /// - /// Unit representation to use. /// String representation. "@; # Windows Runtime Component does not support IFormatProvider type if ($wrc) {@" /// Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to if null. - public string ToString($unitEnumName unit, [CanBeNull] string cultureName) + public string ToString([CanBeNull] string cultureName) { var provider = cultureName; "@; } else {@" /// Format to use for localization and number formatting. Defaults to if null. - public string ToString($unitEnumName unit, [CanBeNull] IFormatProvider provider) + public string ToString([CanBeNull] IFormatProvider provider) { "@; }@" - return ToString(unit, provider, 2); + return ToString(provider, 2); } /// /// Get string representation of value and unit. /// - /// Unit representation to use. /// The number of significant digits after the radix point. /// String representation. "@; # Windows Runtime Component does not support IFormatProvider type if ($wrc) {@" /// Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to if null. - public string ToString($unitEnumName unit, [CanBeNull] string cultureName, int significantDigitsAfterRadix) + public string ToString(string cultureName, int significantDigitsAfterRadix) { var provider = cultureName; "@; } else {@" /// Format to use for localization and number formatting. Defaults to if null. - public string ToString($unitEnumName unit, [CanBeNull] IFormatProvider provider, int significantDigitsAfterRadix) + public string ToString([CanBeNull] IFormatProvider provider, int significantDigitsAfterRadix) { "@; }@" - double value = As(unit); - string format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix); - return ToString(unit, provider, format); + var value = Convert.ToDouble(Value); + var format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix); + return ToString(provider, format); } /// /// Get string representation of value and unit. /// - /// Unit representation to use. /// String format to use. Default: "{0:0.##} {1} for value and unit abbreviation respectively." /// Arguments for string format. Value and unit are implictly included as arguments 0 and 1. /// String representation. "@; # Windows Runtime Component does not support IFormatProvider type if ($wrc) {@" /// Name of culture (ex: "en-US") to use for localization and number formatting. Defaults to if null. - public string ToString($unitEnumName unit, [CanBeNull] string cultureName, [NotNull] string format, [NotNull] params object[] args) + public string ToString([CanBeNull] string cultureName, [NotNull] string format, [NotNull] params object[] args) { - IFormatProvider provider = GetFormatProviderFromCultureName(cultureName); + var provider = GetFormatProviderFromCultureName(cultureName); "@; } else {@" /// Format to use for localization and number formatting. Defaults to if null. - public string ToString($unitEnumName unit, [CanBeNull] IFormatProvider provider, [NotNull] string format, [NotNull] params object[] args) + public string ToString([CanBeNull] IFormatProvider provider, [NotNull] string format, [NotNull] params object[] args) { "@; }@" if (format == null) throw new ArgumentNullException(nameof(format)); @@ -255,8 +242,8 @@ if ($obsoleteAttribute) provider = provider ?? GlobalConfiguration.DefaultCulture; - double value = As(unit); - object[] formatArgs = UnitFormatter.GetFormatArgs(unit, value, provider, args); + var value = Convert.ToDouble(Value); + var formatArgs = UnitFormatter.GetFormatArgs(Unit, value, provider, args); return string.Format(provider, format, formatArgs); }