Skip to main content

Convert

Converts a file size value from one unit to another.

Signature​

double Convert(double size, FileSizeUnit fromUnit, FileSizeUnit toUnit)

Parameters​

ParameterTypeDescription
sizedoubleThe size value to convert. Must be ≥ 0.
fromUnitFileSizeUnitThe unit of the input value.
toUnitFileSizeUnitThe desired output unit.

Returns​

The converted value expressed in toUnit.

Exceptions​

ExceptionCondition
ArgumentExceptionsize is negative.
NotSupportedExceptionAn unrecognised unit is supplied.

Examples​

var vfs = new ValiFileSize();

// Megabytes → Kilobytes
double kb = vfs.Convert(1.5, FileSizeUnit.Megabytes, FileSizeUnit.Kilobytes);
// → 1536

// Gigabytes → Bytes
double bytes = vfs.Convert(1, FileSizeUnit.Gigabytes, FileSizeUnit.Bytes);
// → 1_073_741_824

// Bytes → Megabytes
double mb = vfs.Convert(5_242_880, FileSizeUnit.Bytes, FileSizeUnit.Megabytes);
// → 5

// IEC: Gibibytes → Mebibytes
double mib = vfs.Convert(2, FileSizeUnit.Gibibytes, FileSizeUnit.Mebibytes);
// → 2048

Internals​

All values normalize to bytes internally, then divide by the target unit's multiplier (1 KB = 1 024 bytes).