From 4ebe4b8302cef3eda2e9a5bb97e334b3e9075f6c Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Tue, 12 Feb 2019 23:26:15 +0100 Subject: [PATCH 1/3] Use formatprovider in StonePounds.ToString() --- UnitsNet.Tests/CustomCode/StonePoundsTests.cs | 32 ++++++++++++++++--- UnitsNet/CustomCode/Quantities/Mass.extra.cs | 7 ++-- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/UnitsNet.Tests/CustomCode/StonePoundsTests.cs b/UnitsNet.Tests/CustomCode/StonePoundsTests.cs index 977d0a13f0..915eb08e7c 100644 --- a/UnitsNet.Tests/CustomCode/StonePoundsTests.cs +++ b/UnitsNet.Tests/CustomCode/StonePoundsTests.cs @@ -1,16 +1,16 @@ // Copyright (c) 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). // https://github.com/angularsen/UnitsNet -// +// // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: -// +// // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. -// +// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -19,6 +19,7 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. +using System.Globalization; using Xunit; namespace UnitsNet.Tests.CustomCode @@ -46,5 +47,28 @@ public void StonePoundsRoundTrip() AssertEx.EqualTolerance(2, stonePounds.Stone, StoneTolerance); AssertEx.EqualTolerance(3, stonePounds.Pounds, PoundsTolerance); } + + [Fact] + public void StonePoundsToString_FormatsNumberInDefaultCulture() + { + Mass m = Mass.FromStonePounds(3500, 1); + StonePounds stonePounds = m.StonePounds; + string numberInCurrentCulture = 3500.ToString("n0", GlobalConfiguration.DefaultCulture); // Varies between machines, can't hard code it + + Assert.Equal($"{numberInCurrentCulture} st 1 lb", stonePounds.ToString()); + } + + // These cultures use a thin space in digit grouping + [Theory] + [InlineData("nn-NO")] + [InlineData("fr-FR")] + public void StonePoundsToString_GivenCultureWithThinSpaceDigitGroup_ReturnsNumberWithThinSpaceDigitGroup(string cultureName) + { + var formatProvider = new CultureInfo(cultureName); + Mass m = Mass.FromStonePounds(3500, 1); + StonePounds stonePounds = m.StonePounds; + + Assert.Equal("3 500 st 1 lb", stonePounds.ToString(formatProvider)); + } } -} \ No newline at end of file +} diff --git a/UnitsNet/CustomCode/Quantities/Mass.extra.cs b/UnitsNet/CustomCode/Quantities/Mass.extra.cs index d6a33c9c0b..7dba973eec 100644 --- a/UnitsNet/CustomCode/Quantities/Mass.extra.cs +++ b/UnitsNet/CustomCode/Quantities/Mass.extra.cs @@ -114,10 +114,11 @@ public string ToString([CanBeNull] IFormatProvider cultureInfo) // Note that it isn't customary to use fractions - one wouldn't say "I am 11 stone and 4.5 pounds". // So pounds are rounded here. - var stoneUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(MassUnit.Stone); - var poundUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(MassUnit.Pound); + cultureInfo = cultureInfo ?? GlobalConfiguration.DefaultCulture; + var stoneUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(MassUnit.Stone, cultureInfo); + var poundUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(MassUnit.Pound, cultureInfo); - return string.Format(GlobalConfiguration.DefaultCulture, "{0:n0} {1} {2:n0} {3}", + return string.Format(cultureInfo, "{0:n0} {1} {2:n0} {3}", Stone, stoneUnit, Math.Round(Pounds), poundUnit); } } From bcda98dfe2a537fa8df4dbf15972331962861643 Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Wed, 13 Feb 2019 22:04:28 +0100 Subject: [PATCH 2/3] Fix FeetInches too --- UnitsNet/CustomCode/Quantities/Length.extra.cs | 9 +++++---- UnitsNet/CustomCode/Quantities/Mass.extra.cs | 9 ++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/UnitsNet/CustomCode/Quantities/Length.extra.cs b/UnitsNet/CustomCode/Quantities/Length.extra.cs index 001393d172..8f5f74f0ea 100644 --- a/UnitsNet/CustomCode/Quantities/Length.extra.cs +++ b/UnitsNet/CustomCode/Quantities/Length.extra.cs @@ -191,13 +191,14 @@ public override string ToString() public string ToString([CanBeNull] IFormatProvider cultureInfo) { - // Note that it isn't customary to use fractions - one wouldn't say "I am 5 feet and 4.5 inches". - // So inches are rounded when converting from base units to feet/inches. + cultureInfo = cultureInfo ?? GlobalConfiguration.DefaultCulture; + var footUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(LengthUnit.Foot); var inchUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(LengthUnit.Inch); - return string.Format(GlobalConfiguration.DefaultCulture, "{0:n0} {1} {2:n0} {3}", Feet, footUnit, Math.Round(Inches), - inchUnit); + // Note that it isn't customary to use fractions - one wouldn't say "I am 5 feet and 4.5 inches". + // So inches are rounded when converting from base units to feet/inches. + return string.Format(cultureInfo, "{0:n0} {1} {2:n0} {3}", Feet, footUnit, Math.Round(Inches), inchUnit); } } } diff --git a/UnitsNet/CustomCode/Quantities/Mass.extra.cs b/UnitsNet/CustomCode/Quantities/Mass.extra.cs index 7dba973eec..f030c7da75 100644 --- a/UnitsNet/CustomCode/Quantities/Mass.extra.cs +++ b/UnitsNet/CustomCode/Quantities/Mass.extra.cs @@ -111,15 +111,14 @@ public override string ToString() public string ToString([CanBeNull] IFormatProvider cultureInfo) { - // Note that it isn't customary to use fractions - one wouldn't say "I am 11 stone and 4.5 pounds". - // So pounds are rounded here. - cultureInfo = cultureInfo ?? GlobalConfiguration.DefaultCulture; + var stoneUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(MassUnit.Stone, cultureInfo); var poundUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(MassUnit.Pound, cultureInfo); - return string.Format(cultureInfo, "{0:n0} {1} {2:n0} {3}", - Stone, stoneUnit, Math.Round(Pounds), poundUnit); + // Note that it isn't customary to use fractions - one wouldn't say "I am 11 stone and 4.5 pounds". + // So pounds are rounded here. + return string.Format(cultureInfo, "{0:n0} {1} {2:n0} {3}", Stone, stoneUnit, Math.Round(Pounds), poundUnit); } } } From de8951aa732072dd044db09bcda83ee56eaae246 Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Wed, 13 Feb 2019 22:09:41 +0100 Subject: [PATCH 3/3] I am blind and tmilnthorp has eyes like a hawk --- UnitsNet/CustomCode/Quantities/Length.extra.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/UnitsNet/CustomCode/Quantities/Length.extra.cs b/UnitsNet/CustomCode/Quantities/Length.extra.cs index 8f5f74f0ea..9d139ce62c 100644 --- a/UnitsNet/CustomCode/Quantities/Length.extra.cs +++ b/UnitsNet/CustomCode/Quantities/Length.extra.cs @@ -193,8 +193,8 @@ public string ToString([CanBeNull] IFormatProvider cultureInfo) { cultureInfo = cultureInfo ?? GlobalConfiguration.DefaultCulture; - var footUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(LengthUnit.Foot); - var inchUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(LengthUnit.Inch); + var footUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(LengthUnit.Foot, cultureInfo); + var inchUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(LengthUnit.Inch, cultureInfo); // Note that it isn't customary to use fractions - one wouldn't say "I am 5 feet and 4.5 inches". // So inches are rounded when converting from base units to feet/inches.