Parse file sizes in KB, MB, GB, and so forth in C#


The example Format file sizes in KB, MB, GB, and so forth in C# shows how to convert a number into a string formatted in KB, MB, etc. This example does the opposite: it parses a value such as “1.23 TB” and returns a number.

The following ParseFileSize method does all of the interesting work.

// Parse a file size.
private string[] SizeSuffixes =
    { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
private double ParseFileSize(string value, int kb_value)
    // Remove leading and trailing spaces.
    value = value.Trim();

        // Find the last non-alphabetic character.
        int ext_start = 0;
        for (int i = value.Length - 1; i >= 0; i--)
            // Stop if we find something other than a letter.
            if (!char.IsLetter(value, i))
                ext_start = i + 1;

        // Get the numeric part.
        double number = double.Parse(value.Substring(0, ext_start));

        // Get the extension.
        string suffix;
        if (ext_start < value.Length)
            suffix = value.Substring(ext_start).Trim().ToUpper();
            if (suffix == "BYTES") suffix = "bytes";
            suffix = "bytes";

        // Find the extension in the list.
        int suffix_index = -1;
        for (int i = 0; i < SizeSuffixes.Length; i++)
            if (SizeSuffixes[i] == suffix)
                suffix_index = i;
        if (suffix_index < 0)
            throw new FormatException(
                "Unknown file size extension " + suffix + ".");

        // Return the result.
        return Math.Round(number * Math.Pow(kb_value, suffix_index));
    catch (Exception ex)
        throw new FormatException("Invalid file size format", ex);

The SizeSuffixes array holds suffixes such as KB and GB that represent powers of 1024 in a file size value.

The ParseFileSize method starts by finding the last non-alphabetic character in the string and separating the numeric beginning of the string from the alphabetic suffix at the end.

The method then uses double.Parse to parse the numeric piece.

It then finds the index of the suffix in the suffix list and uses Math.Pow to raise the value kb_value to the power of the index.

The parameter kb_value should be either 1,000 or 1,024 depending on whether the program wants to parse the value with 1,000 byte kilobytes or 1,024 byte kilobytes. Disk drive manufacturers usually use 1,000 byte kilobytes to make you think you’re getting a bigger disk drive than you really are.

For example, suppose the string is “1.2 MB” and kb_value is 1,024. Then the index of the suffix MB is 2 so the result is 1.2 * Math.Pow(1024, 2) = 1.2 * 10242 = 1,258,291.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in algorithms, files, mathematics and tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *