mirror of
https://github.com/SabreTools/SabreTools.IO.git
synced 2026-11-15 16:41:58 +00:00
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:
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user