Skip to content

Commit

Permalink
primary constructor for ByteSize (#1385)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Feb 16, 2024
1 parent 280ddca commit 75a0a52
Showing 1 changed file with 12 additions and 19 deletions.
31 changes: 12 additions & 19 deletions src/Humanizer/Bytes/ByteSize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 75a0a52

Please sign in to comment.