Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

primary constructor for ByteSize #1385

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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