Skip to content

Commit 500c946

Browse files
igiturandersnm
authored andcommitted
Implement parser for currencies
1 parent a90336b commit 500c946

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/ExcelNumberFormat/Parser.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ private static Section ParseSection(Tokenizer reader, int index, out bool syntax
7272
condition = parseCondition;
7373
else if (TryParseColor(expression, out var parseColor))
7474
color = parseColor;
75+
else if (TryParseCurrencySymbol(expression, out var parseCurrencySymbol))
76+
tokens.Add("\"" + parseCurrencySymbol + "\"");
7577
}
7678
else
7779
{
@@ -374,5 +376,23 @@ private static bool TryParseColor(string token, out Color color)
374376
color = null;
375377
return false;
376378
}
379+
380+
private static bool TryParseCurrencySymbol(string token, out string currencySymbol)
381+
{
382+
if (string.IsNullOrEmpty(token)
383+
|| !token.StartsWith("$"))
384+
{
385+
currencySymbol = null;
386+
return false;
387+
}
388+
389+
390+
if (token.Contains("-"))
391+
currencySymbol = token.Substring(1, token.IndexOf('-') - 1);
392+
else
393+
currencySymbol = token.Substring(1);
394+
395+
return true;
396+
}
377397
}
378398
}

test/ExcelNumberFormat.Tests/Class1.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,5 +900,12 @@ public void TestEmptyFormatString()
900900
result = Format(new DateTime(2017, 10, 28), string.Empty, CultureInfo.InvariantCulture);
901901
Assert.AreEqual("10/28/2017 00:00:00", result);
902902
}
903+
904+
[TestMethod]
905+
public void TestCurrency()
906+
{
907+
Test(1234.56, "[$€-1809]# ##0.00", "€1 234.56");
908+
Test(1234.56, "#,##0.00 [$EUR]", "1,234.56 EUR");
909+
}
903910
}
904911
}

0 commit comments

Comments
 (0)