Skip to content

Commit 22ddd82

Browse files
authored
[Conditional Formatter] Use invariant decimal parsing (#456)
* Added test to ensure that Choose decimal parsing uses invariant culture. * Use invariant culture when parsing the choose decimal.
1 parent b4fd639 commit 22ddd82

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/SmartFormat.Tests/Extensions/ConditionalFormatterTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Globalization;
23
using NUnit.Framework;
34
using SmartFormat.Core.Formatting;
45
using SmartFormat.Extensions;
@@ -159,4 +160,27 @@ public void Should_Process_Signed_And_Unsigned_Numbers()
159160
Assert.That(smart.Format("{0:cond:=123?yes|no}", number), Is.EqualTo("yes"));
160161
}
161162
}
163+
164+
[TestCase("en")]
165+
[TestCase("fr")]
166+
[TestCase("nb-NO")]
167+
public void Test_DecimalParsingUses_InvariantCulture(string culture)
168+
{
169+
const string format = "{0:cond:<=0.25?I am less than 0.25|I am over 0.25}";
170+
const string expected = "I am over 0.25";
171+
172+
var currentCulture = CultureInfo.CurrentCulture;
173+
var cultureInfo = CultureInfo.CreateSpecificCulture(culture);
174+
CultureInfo.CurrentCulture = cultureInfo;
175+
176+
try
177+
{
178+
var smart = Smart.CreateDefaultSmartFormat();
179+
smart.Test(format, new object[] { 0.3 }, expected);
180+
}
181+
finally
182+
{
183+
CultureInfo.CurrentCulture = currentCulture;
184+
}
185+
}
162186
}

src/SmartFormat/Extensions/ConditionalFormatter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Collections.Generic;
7+
using System.Globalization;
78
using System.Text.RegularExpressions;
89
using SmartFormat.Core.Extensions;
910
using SmartFormat.Core.Parsing;
@@ -199,7 +200,7 @@ private static bool TryEvaluateCondition(Format parameter, decimal value, out bo
199200

200201
for (var i = 0; i < andOrs.Count; i++)
201202
{
202-
var v = decimal.Parse(values[i].Value);
203+
var v = decimal.Parse(values[i].Value, CultureInfo.InvariantCulture);
203204
var exp = false;
204205
switch (comps[i].Value)
205206
{

0 commit comments

Comments
 (0)