Skip to content

Commit 79faa5f

Browse files
MikalaiMazurenkaDmitryLukyanov
authored andcommitted
CSHARP-2821: Decimal128.ToDouble() fails if culture is FR
1 parent 0a9e6dd commit 79faa5f

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/MongoDB.Bson/ObjectModel/Decimal128.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ public static double ToDouble(Decimal128 d)
793793
{
794794
// TODO: implement this more efficiently
795795
var stringValue = d.ToString();
796-
return double.Parse(stringValue);
796+
return double.Parse(stringValue, CultureInfo.InvariantCulture);
797797
}
798798
else if (Flags.IsSecondForm(d._highBits))
799799
{
@@ -945,7 +945,7 @@ public static float ToSingle(Decimal128 d)
945945
{
946946
// TODO: implement this more efficiently
947947
var stringValue = d.ToString();
948-
return float.Parse(stringValue);
948+
return float.Parse(stringValue, CultureInfo.InvariantCulture);
949949
}
950950
else if (Flags.IsSecondForm(d._highBits))
951951
{

tests/MongoDB.Bson.Tests/ObjectModel/Decimal128Tests.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System;
1717
using FluentAssertions;
1818
using System.Globalization;
19+
using System.Threading;
1920
using Xunit;
2021

2122
namespace MongoDB.Bson.Tests
@@ -121,6 +122,46 @@ public void ToDecimal_should_throw_when_value_is_out_of_range(string valueString
121122
exception.Should().BeOfType<OverflowException>();
122123
}
123124

125+
[Theory]
126+
[InlineData("123.456", 123.456)]
127+
public void ToDouble_should_not_depend_on_current_culture(string valueString, double expectedDouble)
128+
{
129+
var currentCulture = Thread.CurrentThread.CurrentCulture;
130+
try
131+
{
132+
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
133+
var subject = Decimal128.Parse(valueString);
134+
135+
var result = Decimal128.ToDouble(subject);
136+
137+
result.Should().Be(expectedDouble);
138+
}
139+
finally
140+
{
141+
Thread.CurrentThread.CurrentCulture = currentCulture;
142+
}
143+
}
144+
145+
[Theory]
146+
[InlineData("123.456", 123.456)]
147+
public void ToSingle_should_not_depend_on_current_culture(string valueString, float expectedFloat)
148+
{
149+
var currentCulture = Thread.CurrentThread.CurrentCulture;
150+
try
151+
{
152+
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
153+
var subject = Decimal128.Parse(valueString);
154+
155+
var result = Decimal128.ToSingle(subject);
156+
157+
result.Should().Be(expectedFloat);
158+
}
159+
finally
160+
{
161+
Thread.CurrentThread.CurrentCulture = currentCulture;
162+
}
163+
}
164+
124165
[Theory]
125166
[InlineData((byte)0, "0")]
126167
[InlineData((byte)1, "1")]

0 commit comments

Comments
 (0)