Skip to content

Commit

Permalink
Formatting money with F now respects currency decimal digits #46
Browse files Browse the repository at this point in the history
  • Loading branch information
remyvd committed Jul 13, 2019
1 parent 5bf26b6 commit 8e0a22c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
7 changes: 6 additions & 1 deletion src/NodaMoney/Money.Formattable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,14 @@ private string ConvertToString(string format, IFormatProvider formatProvider)
format = "C";
}

if (format.StartsWith("F", StringComparison.CurrentCulture))
if (format.StartsWith("F", StringComparison.Ordinal))
{
format = format.Replace("F", "N");
if (format.Length == 1)
{
format += Currency.DecimalDigits;
}

return $"{Amount.ToString(format, provider)} {Currency.EnglishName}";
}

Expand Down
8 changes: 4 additions & 4 deletions tests/NodaMoney.Tests/MoneyFormattableSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,21 +316,21 @@ public class GivenIWantMoneyAsStringWithEnglishCurrencyName
public void WhenCurrentCulturePTBR_ThenDecimalsFollowsCurrencyAndAmountFollowsCurrentCulturePtBRAndCurrencyNameIsInEnglish()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("pt-BR");
_yen.ToString("F").Should().Be("765,00 Japanese yen");
_yen.ToString("F").Should().Be("765 Japanese yen");
_euro.ToString("F").Should().Be("765,43 Euro");
_dollar.ToString("F").Should().Be("765,43 United States dollar");
_dinar.ToString("F").Should().Be("765,43 Bahraini dinar");
_dinar.ToString("F").Should().Be("765,432 Bahraini dinar");
}

[Fact]
[UseCulture("en-US")]
public void WhenCurrentCultureEnUS_ThenDecimalsFollowsCurrencyAndAmountFollowsCurrentCultureEnUSAndCurrencyNameIsInEnglish()
{
Thread.CurrentThread.CurrentCulture.Name.Should().Be("en-US");
_yen.ToString("F").Should().Be("765.00 Japanese yen");
_yen.ToString("F").Should().Be("765 Japanese yen");
_euro.ToString("F").Should().Be("765.43 Euro");
_dollar.ToString("F").Should().Be("765.43 United States dollar");
_dinar.ToString("F").Should().Be("765.43 Bahraini dinar");
_dinar.ToString("F").Should().Be("765.432 Bahraini dinar");
}

[Fact]
Expand Down

0 comments on commit 8e0a22c

Please sign in to comment.