From 792b4217405571aec18783e044eb20b27994b03a Mon Sep 17 00:00:00 2001 From: menes97 <38889017+menes97@users.noreply.github.com> Date: Wed, 18 Nov 2026 22:11:25 +0300 Subject: [PATCH] fix: NumberInput parses and writes its text with the invariant culture The HTML number input always exchanges its text with '.' as the decimal separator, but UINumberInput.Value parsed it, and GUI.Value wrote it, with the current culture. In cultures such as tr-TR, "1.5" was read as 15 and Value(1.5) wrote "1,5", which the number input rejects. Co-Authored-By: Claude Opus 5.5 --- .../Tool/GUI/Components/IUINumberInput.cs | 8 ++-- .../Tool/GUI/Components/UINumberInputTests.cs | 44 +++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 src/app/tests/DevToys.UnitTests/Tool/GUI/Components/UINumberInputTests.cs diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUINumberInput.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUINumberInput.cs index a4b05a7f5b..ade1e5e59a 100644 --- a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUINumberInput.cs +++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUINumberInput.cs @@ -1,4 +1,6 @@ -namespace DevToys.Api; +using System.Globalization; + +namespace DevToys.Api; /// /// A component that can be used to display or edit numbers on a single line. @@ -80,7 +82,7 @@ public double Value { get { - if (double.TryParse(Text, out double value)) + if (double.TryParse(Text, NumberStyles.Float, CultureInfo.InvariantCulture, out double value)) { return Math.Min(Math.Max(value, Min), Max); } @@ -160,7 +162,7 @@ public static IUINumberInput Step(this IUINumberInput element, double step) public static IUINumberInput Value(this IUINumberInput element, double value) { double safeValue = Math.Min(Math.Max(value, element.Min), element.Max); - element.Text(safeValue.ToString()); + element.Text(safeValue.ToString(CultureInfo.InvariantCulture)); return element; } diff --git a/src/app/tests/DevToys.UnitTests/Tool/GUI/Components/UINumberInputTests.cs b/src/app/tests/DevToys.UnitTests/Tool/GUI/Components/UINumberInputTests.cs new file mode 100644 index 0000000000..ebbddb2ce0 --- /dev/null +++ b/src/app/tests/DevToys.UnitTests/Tool/GUI/Components/UINumberInputTests.cs @@ -0,0 +1,44 @@ +using System.Globalization; + +namespace DevToys.UnitTests.Tool.GUI.Components; + +public class UINumberInputTests +{ + [Fact] + public void Value_ParsesTextWithInvariantCulture() + { + // The HTML number input always provides its text with '.' as the decimal separator. + CultureInfo originalCulture = CultureInfo.CurrentCulture; + try + { + CultureInfo.CurrentCulture = new CultureInfo("tr-TR"); + + IUINumberInput numberInput = DevToys.Api.GUI.NumberInput("id").Text("1.5"); + + Assert.Equal(1.5, numberInput.Value); + } + finally + { + CultureInfo.CurrentCulture = originalCulture; + } + } + + [Fact] + public void ValueExtension_WritesTextWithInvariantCulture() + { + CultureInfo originalCulture = CultureInfo.CurrentCulture; + try + { + CultureInfo.CurrentCulture = new CultureInfo("tr-TR"); + + IUINumberInput numberInput = DevToys.Api.GUI.NumberInput("id").Value(1.5); + + Assert.Equal("1.5", numberInput.Text); + Assert.Equal(1.5, numberInput.Value); + } + finally + { + CultureInfo.CurrentCulture = originalCulture; + } + } +}