Class MathExtensions
Formatting and small numeric helpers — human-friendly renderings of sizes, counts, durations, and bandwidth, plus a few power-of-two utilities. Formatting
uses a fixed en-us culture so output is stable regardless of the host locale.
public static class MathExtensions
- Inheritance
-
MathExtensions
- Inherited Members
Methods
Bandwidth(int, double)
Formats a throughput as a human-readable rate — size divided by elapsed seconds, scaled 1024-based with
bit-style units, e.g. 1.5Mb/sec.
public static string Bandwidth(int size, double elapsed)
Parameters
Returns
- string
The rate string, in the fixed
en-usculture.
Bandwidth(long, double)
Formats a throughput as a human-readable rate — size divided by elapsed seconds, scaled 1024-based with
bit-style units, e.g. 1.5Mb/sec.
public static string Bandwidth(long size, double elapsed)
Parameters
Returns
- string
The rate string, in the fixed
en-usculture.
FriendlyAmount(double)
Formats a count with a 1000-based scale suffix (none, K, M, G).
public static string FriendlyAmount(this double val)
Parameters
valdoubleThe value to format.
Returns
- string
The formatted string, in the fixed
en-usculture.
FriendlyAmount(int)
Formats a count with a 1000-based scale suffix (none, K, M, B), e.g. 1.5M for 1,500,000.
public static string FriendlyAmount(this int val)
Parameters
valintThe value to format.
Returns
- string
The formatted string, in the fixed
en-usculture.
FriendlySize(double)
Formats a value with a 1024-based scale using bit-style units (b, Kb, Mb, Gb).
public static string FriendlySize(this double val)
Parameters
valdoubleThe value to format.
Returns
- string
The formatted string, in the fixed
en-usculture.
FriendlySize(int)
Formats a byte/element count with a 1024-based scale suffix (none, K, M, B), e.g. 1.5M.
public static string FriendlySize(this int val)
Parameters
valintThe value to format.
Returns
- string
The formatted string, in the fixed
en-usculture.
FriendlySize(long)
Formats a byte/element count with a 1024-based scale suffix (none, K, M, B), e.g. 1.5M.
public static string FriendlySize(this long val)
Parameters
vallongThe value to format.
Returns
- string
The formatted string, in the fixed
en-usculture.
FriendlyTime(double, bool)
Formats a duration (in seconds) with the largest fitting unit — sec, ms, µs, or ns. When displayRate is
true, appends the reciprocal as a per-second rate, e.g. 1.5µs (666.667K/sec).
public static string FriendlyTime(this double val, bool displayRate = true)
Parameters
valdoubleThe duration, in seconds.
displayRateboolWhen
true, also append the1/valrate in parentheses.
Returns
- string
The formatted string, in the fixed
en-usculture.
IsPowerOf2(int)
Whether x is a power of two. Returns true for 0.
public static bool IsPowerOf2(this int x)
Parameters
xintThe value to test.
Returns
- bool
trueifxhas at most one bit set.
IsPowerOf2(long)
Whether x is a power of two. Returns true for 0.
public static bool IsPowerOf2(this long x)
Parameters
xlongThe value to test.
Returns
- bool
trueifxhas at most one bit set.
NextPowerOf2(int)
Return the next power of 2 of the given value
public static int NextPowerOf2(this int v)
Parameters
vintThe value
Returns
- int
The next power of 2
Remarks
If the given value is already a power of 2, this method will return the next one.
TicksToSeconds(long)
Converts TimeSpan ticks to seconds.
public static double TicksToSeconds(this long ticks)
Parameters
tickslongA tick count (100 ns units).
Returns
- double
The equivalent number of seconds.
TotalSeconds(int)
Converts TimeSpan ticks to total seconds via FromTicks(long).
public static double TotalSeconds(this int ticks)
Parameters
ticksintA tick count (100 ns units).
Returns
- double
The equivalent number of seconds.
TotalSeconds(long)
Converts TimeSpan ticks to total seconds via FromTicks(long).
public static double TotalSeconds(this long ticks)
Parameters
tickslongA tick count (100 ns units).
Returns
- double
The equivalent number of seconds.