diff --git a/src/Umbraco.Core/PropertyEditors/DecimalPropertyEditor.cs b/src/Umbraco.Core/PropertyEditors/DecimalPropertyEditor.cs index 5d10cf7e76..58c86b1426 100644 --- a/src/Umbraco.Core/PropertyEditors/DecimalPropertyEditor.cs +++ b/src/Umbraco.Core/PropertyEditors/DecimalPropertyEditor.cs @@ -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, + }; /// /// Base validator for the decimal property editor validation against data type configured values. diff --git a/tests/Umbraco.Tests.UnitTests/Umbraco.Core/PropertyEditors/DecimalPropertyValueEditorTests.cs b/tests/Umbraco.Tests.UnitTests/Umbraco.Core/PropertyEditors/DecimalPropertyValueEditorTests.cs index 92b4f8579a..b70523403e 100644 --- a/tests/Umbraco.Tests.UnitTests/Umbraco.Core/PropertyEditors/DecimalPropertyValueEditorTests.cs +++ b/tests/Umbraco.Tests.UnitTests/Umbraco.Core/PropertyEditors/DecimalPropertyValueEditorTests.cs @@ -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() {