From 8db043d96e17236b1bd0cdc96a7de3b557117bdf Mon Sep 17 00:00:00 2001 From: Keith Dahlby Date: Mon, 3 May 2021 13:38:26 -0500 Subject: [PATCH] Parse positive/negative sign --- src/Humanizer.Tests.Shared/Bytes/ParsingTests.cs | 8 ++++++-- src/Humanizer/Bytes/ByteSize.cs | 4 +++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Humanizer.Tests.Shared/Bytes/ParsingTests.cs b/src/Humanizer.Tests.Shared/Bytes/ParsingTests.cs index 9c10f7acb..d1b489c75 100644 --- a/src/Humanizer.Tests.Shared/Bytes/ParsingTests.cs +++ b/src/Humanizer.Tests.Shared/Bytes/ParsingTests.cs @@ -56,10 +56,13 @@ public void TryParse() [Theory] [InlineData("2000.01KB", "")] // Invariant [InlineData("2,000.01KB", "")] + [InlineData("+2000.01KB", "")] [InlineData("2000.01KB", "en")] [InlineData("2,000.01KB", "en")] + [InlineData("+2000.01KB", "en")] [InlineData("2000,01KB", "de")] [InlineData("2.000,01KB", "de")] + [InlineData("+2000,01KB", "de")] public void TryParseWithCultureInfo(string value, string cultureName) { var culture = new CultureInfo(cultureName); @@ -77,12 +80,13 @@ public void TryParseWithNumberFormatInfo() { NumberDecimalSeparator = "_", NumberGroupSeparator = ";", + NegativeSign = "−", // proper minus, not hyphen-minus }; - var value = "2;000_01KB"; + var value = "−2;000_01KB"; Assert.True(ByteSize.TryParse(value, numberFormat, out var resultByteSize)); - Assert.Equal(ByteSize.FromKilobytes(2000.01), resultByteSize); + Assert.Equal(ByteSize.FromKilobytes(-2000.01), resultByteSize); Assert.Equal(resultByteSize, ByteSize.Parse(value, numberFormat)); } diff --git a/src/Humanizer/Bytes/ByteSize.cs b/src/Humanizer/Bytes/ByteSize.cs index 0cab8a7d7..22517c0b9 100644 --- a/src/Humanizer/Bytes/ByteSize.cs +++ b/src/Humanizer/Bytes/ByteSize.cs @@ -485,11 +485,13 @@ public static bool TryParse(string s, IFormatProvider formatProvider, out ByteSi // Acquiring culture-specific parsing info var numberFormat = GetNumberFormatInfo(formatProvider); - const NumberStyles numberStyles = AllowDecimalPoint | AllowThousands; + const NumberStyles numberStyles = AllowDecimalPoint | AllowThousands | AllowLeadingSign; var numberSpecialChars = new[] { Convert.ToChar(numberFormat.NumberDecimalSeparator), Convert.ToChar(numberFormat.NumberGroupSeparator), + Convert.ToChar(numberFormat.PositiveSign), + Convert.ToChar(numberFormat.NegativeSign), }; // Setup the result