From ca26c77d3611a719118ccb142ae96f06246a24d8 Mon Sep 17 00:00:00 2001 From: Simon Cropp <simon.cropp@gmail.com> Date: Sat, 17 Feb 2024 00:11:32 +1100 Subject: [PATCH] primary constructor for ByteSize --- src/Humanizer/Bytes/ByteSize.cs | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/src/Humanizer/Bytes/ByteSize.cs b/src/Humanizer/Bytes/ByteSize.cs index 53dcb5209..4db2b6cf7 100644 --- a/src/Humanizer/Bytes/ByteSize.cs +++ b/src/Humanizer/Bytes/ByteSize.cs @@ -28,7 +28,11 @@ namespace Humanizer /// Represents a byte size value. /// </summary> #pragma warning disable 1591 - public struct ByteSize : IComparable<ByteSize>, IEquatable<ByteSize>, IComparable, IFormattable + public struct ByteSize(double byteSize) : + IComparable<ByteSize>, + IEquatable<ByteSize>, + IComparable, + IFormattable { public static readonly ByteSize MinValue = FromBits(long.MinValue); public static readonly ByteSize MaxValue = FromBits(long.MaxValue); @@ -52,12 +56,12 @@ public struct ByteSize : IComparable<ByteSize>, IEquatable<ByteSize>, IComparabl public const string TerabyteSymbol = "TB"; public const string Terabyte = "terabyte"; - public long Bits { get; } - public double Bytes { get; } - public double Kilobytes { get; } - public double Megabytes { get; } - public double Gigabytes { get; } - public double Terabytes { get; } + public long Bits { get; } = (long)Math.Ceiling(byteSize * BitsInByte); + public double Bytes { get; } = byteSize; + public double Kilobytes { get; } = byteSize / BytesInKilobyte; + public double Megabytes { get; } = byteSize / BytesInMegabyte; + public double Gigabytes { get; } = byteSize / BytesInGigabyte; + public double Terabytes { get; } = byteSize / BytesInTerabyte; public string LargestWholeNumberSymbol => GetLargestWholeNumberSymbol(); @@ -163,18 +167,7 @@ public double LargestWholeNumberValue } } - public ByteSize(double byteSize) - : this() - { - // Get ceiling because bis are whole units - Bits = (long)Math.Ceiling(byteSize * BitsInByte); - - Bytes = byteSize; - Kilobytes = byteSize / BytesInKilobyte; - Megabytes = byteSize / BytesInMegabyte; - Gigabytes = byteSize / BytesInGigabyte; - Terabytes = byteSize / BytesInTerabyte; - } + // Get ceiling because bis are whole units public static ByteSize FromBits(long value) => new(value / (double) BitsInByte);