Skip to main content

FormatBestSize

Converts bytes to the most appropriate unit and returns a formatted string in a single call. Equivalent to calling GetBestUnit followed by FormatSize.

Signature

string FormatBestSize(double bytes, int decimalPlaces = 2, CultureInfo? culture = null, bool useIec = false)

Parameters

ParameterTypeDefaultDescription
bytesdoubleThe size in bytes. Must be ≥ 0.
decimalPlacesint2Number of decimal places. Must be ≥ 0.
cultureCultureInfo?nullCulture for number formatting.
useIecboolfalseWhen true, uses IEC suffixes (KiB, MiB, …).

Returns

A formatted string such as "1.40 GB".

Examples

var vfs = new ValiFileSize();

vfs.FormatBestSize(1_500_000_000);
// → "1.40 GB"

vfs.FormatBestSize(1_500_000_000, decimalPlaces: 3);
// → "1.397 GB"

vfs.FormatBestSize(1_500_000_000, useIec: true);
// → "1.40 GiB"

vfs.FormatBestSize(512);
// → "512.00 B"

vfs.FormatBestSize(2048);
// → "2.00 KB"