From a82a5ede00f2a3701198adb447aa6053aa14c2e5 Mon Sep 17 00:00:00 2001 From: Karl Jones Date: Tue, 17 Dec 2024 11:14:10 +0000 Subject: [PATCH 1/2] Added test to ensure that Choose decimal parsing uses invariant culture. --- .../Extensions/ConditionalFormatterTests.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/SmartFormat.Tests/Extensions/ConditionalFormatterTests.cs b/src/SmartFormat.Tests/Extensions/ConditionalFormatterTests.cs index 209c9c44..b200215e 100644 --- a/src/SmartFormat.Tests/Extensions/ConditionalFormatterTests.cs +++ b/src/SmartFormat.Tests/Extensions/ConditionalFormatterTests.cs @@ -1,4 +1,5 @@ using System; +using System.Globalization; using NUnit.Framework; using SmartFormat.Core.Formatting; using SmartFormat.Extensions; @@ -159,4 +160,27 @@ public void Should_Process_Signed_And_Unsigned_Numbers() Assert.That(smart.Format("{0:cond:=123?yes|no}", number), Is.EqualTo("yes")); } } + + [TestCase("en")] + [TestCase("fr")] + [TestCase("nb-NO")] + public void Test_DecimalParsingUses_InvariantCulture(string culture) + { + const string format = "{0:cond:<=0.25?I am less than 0.25|I am over 0.25}"; + const string expected = "I am over 0.25"; + + var currentCulture = CultureInfo.CurrentCulture; + var cultureInfo = CultureInfo.CreateSpecificCulture(culture); + CultureInfo.CurrentCulture = cultureInfo; + + try + { + var smart = Smart.CreateDefaultSmartFormat(); + smart.Test(format, new object[] { 0.3 }, expected); + } + finally + { + CultureInfo.CurrentCulture = currentCulture; + } + } } From 2a94672fe4634046f7cae4f809b3626b4cd99f28 Mon Sep 17 00:00:00 2001 From: Karl Jones Date: Tue, 17 Dec 2024 11:14:26 +0000 Subject: [PATCH 2/2] Use invariant culture when parsing the choose decimal. --- src/SmartFormat/Extensions/ConditionalFormatter.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/SmartFormat/Extensions/ConditionalFormatter.cs b/src/SmartFormat/Extensions/ConditionalFormatter.cs index 5cedff04..146d7b53 100644 --- a/src/SmartFormat/Extensions/ConditionalFormatter.cs +++ b/src/SmartFormat/Extensions/ConditionalFormatter.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Text.RegularExpressions; using SmartFormat.Core.Extensions; using SmartFormat.Core.Parsing; @@ -199,7 +200,7 @@ private static bool TryEvaluateCondition(Format parameter, decimal value, out bo for (var i = 0; i < andOrs.Count; i++) { - var v = decimal.Parse(values[i].Value); + var v = decimal.Parse(values[i].Value, CultureInfo.InvariantCulture); var exp = false; switch (comps[i].Value) {