Fix DecimalPropertyEditor on non-English systems (#20215)

* Directly convert from double or float when possible. Also fixes string parsing to work on all cultures. Fixes #20214

* Added unit tests to verify behaviour.

---------

Co-authored-by: Andy Butland <abutland73@gmail.com>
This commit is contained in:
Asbjørn Riis-Knudsen
2025-09-23 08:59:08 +02:00
committed by GitHub
parent a6e736849f
commit 493dd1dc78
2 changed files with 26 additions and 5 deletions

View File

@@ -64,11 +64,18 @@ public class DecimalPropertyEditor : DataEditor
=> TryParsePropertyValue(editorValue.Value);
private static decimal? TryParsePropertyValue(object? value)
=> value is decimal decimalValue
? decimalValue
: decimal.TryParse(value?.ToString(), CultureInfo.InvariantCulture, out var parsedDecimalValue)
? parsedDecimalValue
: null;
=> value switch
{
decimal d => d,
double db => (decimal)db,
float f => (decimal)f,
IFormattable f => decimal.TryParse(f.ToString(null, CultureInfo.InvariantCulture), NumberStyles.Any, CultureInfo.InvariantCulture, out var parsedDecimalValue)
? parsedDecimalValue
: null,
_ => decimal.TryParse(value?.ToString(), CultureInfo.CurrentCulture, out var parsedDecimalValue)
? parsedDecimalValue
: null,
};
/// <summary>
/// Base validator for the decimal property editor validation against data type configured values.

View File

@@ -26,8 +26,10 @@ public class DecimalPropertyValueEditorTests
{ 123, 123m },
{ -123, -123m },
{ 123.45d, 123.45m },
{ 123.45f, 123.45m },
{ "123.45", 123.45m },
{ "1234.56", 1234.56m },
{ "1,234.56", 1234.56m },
{ "123,45", 12345m },
{ "1.234,56", null },
{ "123 45", null },
@@ -49,6 +51,18 @@ public class DecimalPropertyValueEditorTests
}
}
[SetCulture("it-IT")]
[SetUICulture("it-IT")]
[TestCase("123,45", 123.45)]
[TestCase("1.234,56", 1234.56)]
[TestCase("123.45", 12345)]
[TestCase("1,234.56", null)]
public void Can_Parse_Values_From_Editor_Using_Culture_With_Non_EnUs_Decimal_Separator(object value, decimal? expected)
{
var fromEditor = FromEditor(value);
Assert.AreEqual(expected, fromEditor);
}
[Test]
public void Can_Parse_Values_To_Editor()
{