From 8e0a22c110e8620d35a1374c4e6c8483689226fc Mon Sep 17 00:00:00 2001 From: remyvd Date: Sat, 13 Jul 2019 09:13:05 +0200 Subject: [PATCH] Formatting money with F now respects currency decimal digits #46 --- src/NodaMoney/Money.Formattable.cs | 7 ++++++- tests/NodaMoney.Tests/MoneyFormattableSpec.cs | 8 ++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/NodaMoney/Money.Formattable.cs b/src/NodaMoney/Money.Formattable.cs index 360542f..a4b95a3 100644 --- a/src/NodaMoney/Money.Formattable.cs +++ b/src/NodaMoney/Money.Formattable.cs @@ -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}"; } diff --git a/tests/NodaMoney.Tests/MoneyFormattableSpec.cs b/tests/NodaMoney.Tests/MoneyFormattableSpec.cs index c6f36b6..1428fd1 100644 --- a/tests/NodaMoney.Tests/MoneyFormattableSpec.cs +++ b/tests/NodaMoney.Tests/MoneyFormattableSpec.cs @@ -316,10 +316,10 @@ 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] @@ -327,10 +327,10 @@ public void WhenCurrentCulturePTBR_ThenDecimalsFollowsCurrencyAndAmountFollowsCu 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]