Skip to content

Commit c54eb3a

Browse files
AndreasLeebAndreas Leeb
andauthored
Fix QuantityTypeConverter ignoring given CurrentCulture without DisplayAsAttribute (#1083)
Co-authored-by: Andreas Leeb <[email protected]>
1 parent 91a4ca8 commit c54eb3a

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

UnitsNet.Tests/QuantityTypeConverterTest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,24 @@ public void ConvertTo_GivenSomeQuantityAndContextWithDisplayAsUnitAttributes_Ret
251251
Assert.Equal("10 dm", convertedQuantitySpecificCulture);
252252
}
253253

254+
[Theory]
255+
[InlineData(true)]
256+
[InlineData(false)]
257+
public void ConvertTo_GivenCurrentCulture_ReturnValueFormattedAccordingToGivenCulture(bool useDisplayAsAttribute)
258+
{
259+
QuantityTypeConverter<Length> converter = new();
260+
var attributes = useDisplayAsAttribute ? new Attribute[] { new DisplayAsUnitAttribute(Units.LengthUnit.Meter) } : Array.Empty<Attribute>();
261+
ITypeDescriptorContext context = new TypeDescriptorContext("SomeMemberName", attributes);
262+
CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("de-AT"); // uses comma as decimal separator
263+
CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture; // uses dot as decimal separator
264+
Length length = Length.FromMeters(1.5);
265+
string expectedResult = length.ToString(CultureInfo.CurrentCulture);
266+
267+
string convertedQuantity = (string)converter.ConvertTo(context, CultureInfo.CurrentCulture, length, typeof(string));
268+
269+
Assert.Equal(expectedResult, convertedQuantity);
270+
}
271+
254272
[Fact]
255273
public void ConvertFrom_GivenDefaultUnitAttributeWithWrongUnitType_ThrowsArgumentException()
256274
{

UnitsNet/QuantityTypeConverter.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,22 @@ public override object ConvertTo(ITypeDescriptorContext context, CultureInfo cul
218218
{
219219
DisplayAsUnitAttribute? displayAsUnit = GetAttribute<DisplayAsUnitAttribute>(context);
220220

221-
if (value is IQuantity qvalue && destinationType == typeof(string) && displayAsUnit != null)
221+
if (value is not IQuantity qvalue || destinationType != typeof(string))
222222
{
223-
if (displayAsUnit.UnitType != null)
224-
return qvalue.ToUnit(displayAsUnit.UnitType).ToString(displayAsUnit.Format, culture);
225-
else
226-
return qvalue.ToString(displayAsUnit.Format, culture);
223+
return base.ConvertTo(context, culture, value, destinationType);
224+
}
225+
226+
if (displayAsUnit == null)
227+
{
228+
return qvalue.ToString(culture);
229+
}
230+
231+
if (displayAsUnit.UnitType == null)
232+
{
233+
return qvalue.ToString(displayAsUnit.Format, culture);
227234
}
228235

229-
return base.ConvertTo(context, culture, value, destinationType);
236+
return qvalue.ToUnit(displayAsUnit.UnitType).ToString(displayAsUnit.Format, culture);
230237
}
231238
}
232239
}

0 commit comments

Comments
 (0)