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.1.0+e86ae44c345422a1011f7fa7b6b715bf3d1a34ce
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 exbibytes 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 "42TB".
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'. For example, 1,572,864 bytes would be formatted as "1536KiB" ("AiB"), "1536Ki" ("Ai"), "1536KB" ("AB"), or "1536K" ("A").
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" ("aB"), or "1500k" ("a").
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" ("SiB"), "1.5Mi" ("Si"), "1.5MB" ("SB") or "1.5M" ("S").
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" (sB), or "1.5M" (s).
byte, K[i]byte, M[i]byte, G[i]byte, T[i]byte, P[i]byte, E[i]byte, A[i]byte, S[i]byte Format the output using the specified or automatic unit, using the long form of the unit (e.g. "kilobytes" or "mebibytes"). IEC prefixes will be used if the 'i' is present, and all prefixes are treated as binary prefixes.
kbyte, mbyte, gbyte, tbyte, pbyte, ebyte, abyte, sbyte Format the output using the specified or automatic SI unit, using the long form of the unit (e.g. "kilobytes" or "megabytes"), and using decimal prefixes.

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.

The actual units used when formatting are determined by the BinaryUnitInfo object provided by the formatProvider, or the value of the BinaryUnitInfoInvariantInfo property if the provider doesn't support this type.

The properties defining abbreviated units, such as BinaryUnitInfoShortByte, BinaryUnitInfoShortKibi or BinaryUnitInfoShortKilo are used for all format strings, except those that end in "byte", which use the full units, defined by properties such as BinaryUnitInfoLongByte, BinaryUnitInfoLongKibi or BinaryUnitInfoLongKilo.

The BinaryUnitInfoShortByte and BinaryUnitInfoLongByte property are only used if the value, when scaled to the prefix, is exactly one. For example, 1 B, 1 KiB, 1 PB, etc. Otherwise, the BinaryUnitInfoShortBytes and BinaryUnitInfoLongBytes properties are used, which provide the plural versions. For the abbreviated units in the default, English-language version, these values are the same (both "B"), but for the full units they are different ("byte" and "bytes").

If the value is not exactly one, but is rounded to one by the number format used, the BinaryUnitInfoLongByte and BinaryUnitInfoLongBytes properties are still used. For example, a value of 1.01 kibibytes, when using a format string of "0.# Sibyte", would be formatted as "1 kibibytes", using the plural version of the unit.

Exceptions

FormatExceptionformat is invalid.

See Also