Skip to content

Commit bb8acc6

Browse files
authored
Use formatprovider in StonePounds and FeetInches ToString() (#606)
* Use formatprovider in StonePounds.ToString() * Fix FeetInches too * I am blind and tmilnthorp has eyes like a hawk
1 parent f437a11 commit bb8acc6

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

UnitsNet.Tests/CustomCode/StonePoundsTests.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// Copyright (c) 2013 Andreas Gullberg Larsen ([email protected]).
22
// https://github.com/angularsen/UnitsNet
3-
//
3+
//
44
// Permission is hereby granted, free of charge, to any person obtaining a copy
55
// of this software and associated documentation files (the "Software"), to deal
66
// in the Software without restriction, including without limitation the rights
77
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
88
// copies of the Software, and to permit persons to whom the Software is
99
// furnished to do so, subject to the following conditions:
10-
//
10+
//
1111
// The above copyright notice and this permission notice shall be included in
1212
// all copies or substantial portions of the Software.
13-
//
13+
//
1414
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1515
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1616
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -19,6 +19,7 @@
1919
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2020
// THE SOFTWARE.
2121

22+
using System.Globalization;
2223
using Xunit;
2324

2425
namespace UnitsNet.Tests.CustomCode
@@ -46,5 +47,28 @@ public void StonePoundsRoundTrip()
4647
AssertEx.EqualTolerance(2, stonePounds.Stone, StoneTolerance);
4748
AssertEx.EqualTolerance(3, stonePounds.Pounds, PoundsTolerance);
4849
}
50+
51+
[Fact]
52+
public void StonePoundsToString_FormatsNumberInDefaultCulture()
53+
{
54+
Mass m = Mass.FromStonePounds(3500, 1);
55+
StonePounds stonePounds = m.StonePounds;
56+
string numberInCurrentCulture = 3500.ToString("n0", GlobalConfiguration.DefaultCulture); // Varies between machines, can't hard code it
57+
58+
Assert.Equal($"{numberInCurrentCulture} st 1 lb", stonePounds.ToString());
59+
}
60+
61+
// These cultures use a thin space in digit grouping
62+
[Theory]
63+
[InlineData("nn-NO")]
64+
[InlineData("fr-FR")]
65+
public void StonePoundsToString_GivenCultureWithThinSpaceDigitGroup_ReturnsNumberWithThinSpaceDigitGroup(string cultureName)
66+
{
67+
var formatProvider = new CultureInfo(cultureName);
68+
Mass m = Mass.FromStonePounds(3500, 1);
69+
StonePounds stonePounds = m.StonePounds;
70+
71+
Assert.Equal("3 500 st 1 lb", stonePounds.ToString(formatProvider));
72+
}
4973
}
50-
}
74+
}

UnitsNet/CustomCode/Quantities/Length.extra.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,14 @@ public override string ToString()
191191

192192
public string ToString([CanBeNull] IFormatProvider cultureInfo)
193193
{
194+
cultureInfo = cultureInfo ?? GlobalConfiguration.DefaultCulture;
195+
196+
var footUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(LengthUnit.Foot, cultureInfo);
197+
var inchUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(LengthUnit.Inch, cultureInfo);
198+
194199
// Note that it isn't customary to use fractions - one wouldn't say "I am 5 feet and 4.5 inches".
195200
// So inches are rounded when converting from base units to feet/inches.
196-
var footUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(LengthUnit.Foot);
197-
var inchUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(LengthUnit.Inch);
198-
199-
return string.Format(GlobalConfiguration.DefaultCulture, "{0:n0} {1} {2:n0} {3}", Feet, footUnit, Math.Round(Inches),
200-
inchUnit);
201+
return string.Format(cultureInfo, "{0:n0} {1} {2:n0} {3}", Feet, footUnit, Math.Round(Inches), inchUnit);
201202
}
202203
}
203204
}

UnitsNet/CustomCode/Quantities/Mass.extra.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,14 @@ public override string ToString()
111111

112112
public string ToString([CanBeNull] IFormatProvider cultureInfo)
113113
{
114-
// Note that it isn't customary to use fractions - one wouldn't say "I am 11 stone and 4.5 pounds".
115-
// So pounds are rounded here.
114+
cultureInfo = cultureInfo ?? GlobalConfiguration.DefaultCulture;
116115

117-
var stoneUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(MassUnit.Stone);
118-
var poundUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(MassUnit.Pound);
116+
var stoneUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(MassUnit.Stone, cultureInfo);
117+
var poundUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(MassUnit.Pound, cultureInfo);
119118

120-
return string.Format(GlobalConfiguration.DefaultCulture, "{0:n0} {1} {2:n0} {3}",
121-
Stone, stoneUnit, Math.Round(Pounds), poundUnit);
119+
// Note that it isn't customary to use fractions - one wouldn't say "I am 11 stone and 4.5 pounds".
120+
// So pounds are rounded here.
121+
return string.Format(cultureInfo, "{0:n0} {1} {2:n0} {3}", Stone, stoneUnit, Math.Round(Pounds), poundUnit);
122122
}
123123
}
124124
}

0 commit comments

Comments
 (0)