From 5fa18e5204f63abb902d286f963f2101d0ebd344 Mon Sep 17 00:00:00 2001 From: Thecentury Date: Wed, 14 Feb 2018 22:24:39 +0300 Subject: [PATCH 1/3] Option to choose format provider for ByteSize --- .../Bytes/ByteSizeExtensionsTests.cs | 15 ++++++++- src/Humanizer/Bytes/ByteSize.cs | 33 ++++++++++++++++--- src/Humanizer/Bytes/ByteSizeExtensions.cs | 23 +++++++++++++ 3 files changed, 66 insertions(+), 5 deletions(-) diff --git a/src/Humanizer.Tests.Shared/Bytes/ByteSizeExtensionsTests.cs b/src/Humanizer.Tests.Shared/Bytes/ByteSizeExtensionsTests.cs index dfa0e032c..53784cf58 100644 --- a/src/Humanizer.Tests.Shared/Bytes/ByteSizeExtensionsTests.cs +++ b/src/Humanizer.Tests.Shared/Bytes/ByteSizeExtensionsTests.cs @@ -1,4 +1,5 @@ -using Humanizer.Bytes; +using System.Globalization; +using Humanizer.Bytes; using Xunit; namespace Humanizer.Tests.Bytes @@ -72,6 +73,18 @@ public void HumanizesTerabytes(double input, string format, string expectedValue Assert.Equal(expectedValue, input.Terabytes().Humanize(format)); } + [Theory] + [InlineData(2.1, null, "en-US", "2.1 TB")] + [InlineData(2.123, "#.#", "en-US", "2.1 TB")] + [InlineData(2.1, null, "ru-RU", "2,1 TB")] + [InlineData(2.123, "#.#", "ru-RU", "2,1 TB")] + public void HumanizeWithFormatProvider(double input, string format, string cultureName, string expectedValue) + { + var culture = new CultureInfo(cultureName); + + Assert.Equal(expectedValue, input.Terabytes().Humanize(format, culture)); + } + [Fact] public void ByteGigabytes() { diff --git a/src/Humanizer/Bytes/ByteSize.cs b/src/Humanizer/Bytes/ByteSize.cs index d4980059a..456ceb829 100644 --- a/src/Humanizer/Bytes/ByteSize.cs +++ b/src/Humanizer/Bytes/ByteSize.cs @@ -21,6 +21,7 @@ //THE SOFTWARE. using System; +using System.Globalization; namespace Humanizer.Bytes { @@ -151,16 +152,40 @@ public static ByteSize FromTerabytes(double value) /// public override string ToString() { - return string.Format("{0} {1}", LargestWholeNumberValue, LargestWholeNumberSymbol); + return ToString(NumberFormatInfo.CurrentInfo); + } + + public string ToString(IFormatProvider provider) + { + if (provider == null) + { + throw new ArgumentNullException(nameof(provider)); + } + + return string.Format("{0} {1}", LargestWholeNumberValue.ToString(provider), LargestWholeNumberSymbol); } public string ToString(string format) { + return ToString(format, NumberFormatInfo.CurrentInfo); + } + + public string ToString(string format, IFormatProvider provider) + { + if (format == null) + { + throw new ArgumentNullException(nameof(format)); + } + if (provider == null) + { + throw new ArgumentNullException(nameof(provider)); + } + if (!format.Contains("#") && !format.Contains("0")) format = "0.## " + format; bool has(string s) => format.IndexOf(s, StringComparison.CurrentCultureIgnoreCase) != -1; - string output(double n) => n.ToString(format); + string output(double n) => n.ToString(format, provider); if (has(TerabyteSymbol)) return output(Terabytes); @@ -178,7 +203,7 @@ public string ToString(string format) if (format.IndexOf(BitSymbol, StringComparison.Ordinal) != -1) return output(Bits); - var formattedLargeWholeNumberValue = LargestWholeNumberValue.ToString(format); + var formattedLargeWholeNumberValue = LargestWholeNumberValue.ToString(format, provider); formattedLargeWholeNumberValue = formattedLargeWholeNumberValue.Equals(string.Empty) ? "0" @@ -333,7 +358,7 @@ public static bool TryParse(string s, out ByteSize result) var found = false; // Acquiring culture specific decimal separator - var decSep = Convert.ToChar(System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator); + var decSep = Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator); // Pick first non-digit number for (num = 0; num < s.Length; num++) diff --git a/src/Humanizer/Bytes/ByteSizeExtensions.cs b/src/Humanizer/Bytes/ByteSizeExtensions.cs index b16ad5992..f272d4d06 100644 --- a/src/Humanizer/Bytes/ByteSizeExtensions.cs +++ b/src/Humanizer/Bytes/ByteSizeExtensions.cs @@ -490,6 +490,29 @@ public static string Humanize(this ByteSize input, string format = null) return string.IsNullOrWhiteSpace(format) ? input.ToString() : input.ToString(format); } + /// + /// Turns a byte quantity into human readable form, eg 2 GB + /// + /// + /// The format provider to use + /// + public static string Humanize(this ByteSize input, IFormatProvider formatProvider) + { + return input.ToString(formatProvider); + } + + /// + /// Turns a byte quantity into human readable form, eg 2 GB + /// + /// + /// The string format to use + /// The format provider to use + /// + public static string Humanize(this ByteSize input, string format, IFormatProvider formatProvider) + { + return string.IsNullOrWhiteSpace(format) ? input.ToString(formatProvider) : input.ToString(format, formatProvider); + } + /// /// Turns a quantity of bytes in a given interval into a rate that can be manipulated /// From 71a875cd2878947961f6f0f89899cf12d54adaf0 Mon Sep 17 00:00:00 2001 From: Thecentury Date: Wed, 14 Feb 2018 22:27:06 +0300 Subject: [PATCH 2/3] Removed brackets for one-liners --- src/Humanizer/Bytes/ByteSize.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Humanizer/Bytes/ByteSize.cs b/src/Humanizer/Bytes/ByteSize.cs index 456ceb829..38c982d0d 100644 --- a/src/Humanizer/Bytes/ByteSize.cs +++ b/src/Humanizer/Bytes/ByteSize.cs @@ -173,13 +173,9 @@ public string ToString(string format) public string ToString(string format, IFormatProvider provider) { if (format == null) - { throw new ArgumentNullException(nameof(format)); - } if (provider == null) - { throw new ArgumentNullException(nameof(provider)); - } if (!format.Contains("#") && !format.Contains("0")) format = "0.## " + format; From e0cc765437c3bb3cb88b4879cf47f3627142cb26 Mon Sep 17 00:00:00 2001 From: Thecentury Date: Wed, 14 Feb 2018 22:35:03 +0300 Subject: [PATCH 3/3] Updated approve_public_api.approved.txt --- .../PublicApiApprovalTest.approve_public_api.approved.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt index f3d98ff29..cdc922717 100644 --- a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt +++ b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt @@ -57,7 +57,9 @@ namespace Humanizer.Bytes public static Humanizer.Bytes.ByteSize Parse(string s) { } public Humanizer.Bytes.ByteSize Subtract(Humanizer.Bytes.ByteSize bs) { } public override string ToString() { } + public string ToString(System.IFormatProvider provider) { } public string ToString(string format) { } + public string ToString(string format, System.IFormatProvider provider) { } public static bool TryParse(string s, out Humanizer.Bytes.ByteSize result) { } } } @@ -90,6 +92,8 @@ namespace Humanizer public static Humanizer.Bytes.ByteSize Gigabytes(this double input) { } public static Humanizer.Bytes.ByteSize Gigabytes(this long input) { } public static string Humanize(this Humanizer.Bytes.ByteSize input, string format = null) { } + public static string Humanize(this Humanizer.Bytes.ByteSize input, System.IFormatProvider formatProvider) { } + public static string Humanize(this Humanizer.Bytes.ByteSize input, string format, System.IFormatProvider formatProvider) { } public static Humanizer.Bytes.ByteSize Kilobytes(this byte input) { } public static Humanizer.Bytes.ByteSize Kilobytes(this sbyte input) { } public static Humanizer.Bytes.ByteSize Kilobytes(this short input) { }