BinarySizeToString(String, IFormatProvider) Method

Formats the value of the current BinarySize instance using the specified format.

Definition

Namespace: Ookii
Assembly: Ookii.BinarySize (in Ookii.BinarySize.dll) Version: 1.0.0+877c9712c6435bf9adfdb0683d7a434c5604a8f2
C#
public string ToString(
	string? format,
	IFormatProvider? formatProvider = null
)

Parameters

format  String
The format to use, or to use the default format.
formatProvider  IFormatProvider  (Optional)
The provider to use to format the value, or to obtain the numeric format information from the current locale setting of the operating system.

Return Value

String
The value of the current instance in the specified format.

Implements

IFormattableToString(String, IFormatProvider)

Remarks

If format is , an empty string, or "G" (the general format specifier), the value will be formatted using the largest binary prefix in which it can be represented without fractions, with the suffix "iB", and a space before the unit. For example, "2 TiB", or "512 B". It is equivalent to using " AiB".

Otherwise, the value of format must be either a multiple-byte unit by itself, or a standard numeric format string or custom numeric format string followed by a multiple-byte unit.

The multiple-byte unit can be one of the following:

Format stringDescription
B The output will be formatted as raw bytes, with the suffix "B", e.g. "512B".
K[i][B], M[i][B], G[i][B], T[i][B], P[i][B], E[i][B] The output will be formatted as kibibytes, mebibytes, gibibytes, tebibytes, pebibytes, or exibytes respectively, with an optional 'i' for IEC units, and an optional 'B'. For these units, SI prefixes without the 'i' character are treated as binary prefixes, so 1 KB equals 1 KiB equals 1,024 bytes, and so on. For example, "1.5KiB", or "2Mi" or "42TB".
k[B], m[B], g[B], t[B], p[B], e[B] With a lower case prefix, the output will be formatted as decimal kilobytes, megabytes, gigabytes, terabytes, petabytes, or exabytes respectively, followed by an optional 'B'. In this case, 1 kB equals 1,000 bytes, 1 MB equals 1,000,000 bytes, and so on. The unit prefix will be capitalized in the output, except for "k" which should be lower case as an SI prefix. For example, "1.5kB", or "2M" or "42T".
A[i][B] Automatically select the largest prefix in which the value can be represented without fractions, optionally followed by an 'i' and/or a 'B'. The former variant uses binary units, while the latter uses decimal. For example, 1,572,864 bytes would be formatted as "1536KiB", "1536Ki", "1536KB", or "1536K"; if using decimal it would be "1572864B", since there is no higher factor.
a[B] Automatically select the largest decimal prefix in which the value can be represented without fractions, optionally followed by a 'B'. For example, 1,500,000 bytes would be formatted as "1500kB", or "1500k".
S[i][B] Automatically select the largest prefix where the value is at least 1, allowing the use of fractional values, optionally followed by an 'i' and/or a 'B'. For example, 1,572,864 bytes would be formatted as "1.5MiB", "1.5Mi", "1.5MB" or "1.5M".
s[B] Automatically select the largest decimal prefix where the value is at least 1, allowing the use of fractional values, optionally followed by a 'B'. For example, 1,500,000 bytes would be formatted as "1.5MB", or "1.5M".

Any of the above multi-byte units may follow a numeric format string; for example, "#,##0.# SiB".

If a multi-byte unit is surrounded by white space, this will be preserved in the output. For example, " KB" can be used to format the value 512 as "0.5 KB".

  Note

Since "G" by itself is the general format specifier, it cannot be used to format as gibibytes; use "GG" instead for this purpose. Using "G" with leading white space or a number format will work correctly.

Exceptions

FormatExceptionformat is invalid.

See Also