Parse doubles with the invariant culture in ConvertToDouble (#5)

* Parse doubles with the invariant culture

ConvertToDouble used double.TryParse with the current culture, so a
value like "59.940000" became 59940000 on a German system and null on a
French one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Drop the culture reset from the ConvertToDouble test

A culture set inside a test does not carry over to the next test, so
the reset and the try/finally around it are not needed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
gmipf
2026-10-15 17:14:53 +02:00
committed by GitHub
parent f1d745471b
commit 19d73bd170
2 changed files with 14 additions and 1 deletions

View File

@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using Xunit;
namespace SabreTools.Text.Extensions.Test
@@ -31,6 +32,17 @@ namespace SabreTools.Text.Extensions.Test
Assert.True(variance < 0.1f);
}
[Theory]
[InlineData("en-US")]
[InlineData("de-DE")]
[InlineData("fr-FR")]
public void ConvertToDoubleTest_CurrentCultureIgnored(string culture)
{
CultureInfo.CurrentCulture = new CultureInfo(culture);
double? actual = NumberHelper.ConvertToDouble("59.940000");
Assert.Equal(59.94, actual);
}
#endregion
#region ConvertToInt64

View File

@@ -1,4 +1,5 @@
using System;
using System.Globalization;
namespace SabreTools.Text.Extensions
{
@@ -47,7 +48,7 @@ namespace SabreTools.Text.Extensions
if (string.IsNullOrEmpty(numeric))
return null;
if (!double.TryParse(numeric, out double doubleValue))
if (!double.TryParse(numeric, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out double doubleValue))
return null;
return doubleValue;