diff --git a/DotBased/Utilities/Culture.cs b/DotBased/Utilities/Culture.cs index 7411011..643fa6e 100755 --- a/DotBased/Utilities/Culture.cs +++ b/DotBased/Utilities/Culture.cs @@ -5,9 +5,9 @@ namespace DotBased.Utilities; public static class Culture { - private static List _sysCultures = new List(); - private static Dictionary _regions = new Dictionary(); - private static readonly ILogger _logger = LogService.RegisterLogger(typeof(Culture)); + private static List _sysCultures = []; + private static readonly Dictionary Regions = new(); + private static readonly ILogger Logger = LogService.RegisterLogger(typeof(Culture)); /// /// Get all system known cultures. @@ -16,7 +16,7 @@ public static class Culture /// The list with 's the system knows public static IEnumerable GetSystemCultures() { - _logger.Debug("Getting system cultures..."); + Logger.Debug("Getting system cultures..."); if (_sysCultures.Count == 0) _sysCultures = CultureInfo.GetCultures(CultureTypes.AllCultures).ToList(); return _sysCultures; @@ -29,16 +29,16 @@ public static class Culture /// A list with regions from the system public static Dictionary GetRegions() { - if (_regions.Count == 0) + if (Regions.Count == 0) { var cultureInfos = GetSystemCultures().Where(cul => !cul.IsNeutralCulture).Where(cul => cul.LCID != 0x7F); foreach (var culture in cultureInfos) { var region = new RegionInfo(culture.Name); - _regions.Add(culture.Name, region); + Regions.Add(culture.Name, region); } } - return _regions; + return Regions; } /// @@ -52,7 +52,7 @@ public static class Culture _sysCultures.Clear(); break; case CacheType.Region: - _regions.Clear(); + Regions.Clear(); break; default: throw new ArgumentOutOfRangeException(nameof(type), type, null); diff --git a/DotBased/Utilities/Suffix.cs b/DotBased/Utilities/Suffix.cs index 60ed314..890f93b 100755 --- a/DotBased/Utilities/Suffix.cs +++ b/DotBased/Utilities/Suffix.cs @@ -12,29 +12,30 @@ public static class Suffix /// Converts the bytes to the memory suffix. /// /// The bytes to convert - /// How manay decimal places will be placed + /// How manny decimal places will be placed /// The suffixed bytes in the correct format public static string BytesToSizeSuffix(long bytes, int decimalPlaces = 1) { - if (decimalPlaces < 0) - decimalPlaces = 1; - switch (bytes) + if (decimalPlaces < 0) decimalPlaces = 1; + + if (bytes == 0) + return $"{0.ToString($"N{decimalPlaces}")} bytes"; + + var negative = bytes < 0; + var absBytes = (ulong)(negative ? -bytes : bytes); + + var mag = (int)Math.Log(absBytes, 1024); + var adjustedSize = absBytes / Math.Pow(1024, mag); + + if (Math.Round(adjustedSize, decimalPlaces) >= 1000 && mag < SizeSuffixes.Length - 1) { - case < 0: - return "-" + BytesToSizeSuffix(-bytes, decimalPlaces); - case 0: - return string.Format("{0:n" + decimalPlaces + "} bytes", 0); - } - - int mag = (int)Math.Log(bytes, 1024); - - decimal adjustedSize = (decimal)bytes / (1L << (mag * 10)); - - if (Math.Round(adjustedSize, decimalPlaces) >= 1000) - { - mag += 1; + mag++; adjustedSize /= 1024; } - return string.Format("{0:n" + decimalPlaces + "} {1}", adjustedSize, SizeSuffixes[mag]); + + var format = $"N{decimalPlaces}"; + var result = $"{adjustedSize.ToString(format)} {SizeSuffixes[mag]}"; + + return negative ? "-" + result : result; } } \ No newline at end of file