Skip to content

Commit

Permalink
[Conditional Formatter] Use invariant decimal parsing (#456)
Browse files Browse the repository at this point in the history
* Added test to ensure that Choose decimal parsing uses invariant culture.

* Use invariant culture when parsing the choose decimal.
  • Loading branch information
karljj1 authored Dec 18, 2024
1 parent b4fd639 commit 22ddd82
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
24 changes: 24 additions & 0 deletions src/SmartFormat.Tests/Extensions/ConditionalFormatterTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using NUnit.Framework;
using SmartFormat.Core.Formatting;
using SmartFormat.Extensions;
Expand Down Expand Up @@ -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;
}
}
}
3 changes: 2 additions & 1 deletion src/SmartFormat/Extensions/ConditionalFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit 22ddd82

Please sign in to comment.