Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/app/dev/DevToys.Api/Tool/GUI/Components/IUINumberInput.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace DevToys.Api;
using System.Globalization;

namespace DevToys.Api;

/// <summary>
/// A component that can be used to display or edit numbers on a single line.
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}