BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Algorithms and Data Structures
.NET 1.1+

Vista-Style File Size Formatter

When copying files, Microsoft Windows Vista shows a dialog box that indicates the size of the remaining items to be processed and the copying speed. The values are formatted with three significant figures. This article explains how to recreate this style.

Scaling the Value

The second step of the process is to scale the number until it can be expressed with three significant digits. This is achieved by repeatedly dividing by 1,024 until the value is less than one thousand. This division occurs within a while loop that also increments a scale value during each iteration.

To add the loop, add the following code to the FormatFileSize method:

decimal filesize = bytes;
SizeScales scale = 0;

while (Math.Round(filesize) >= 1000)
{
    filesize /= 1024;
    scale++;
}

As you can see, the file size is copied into a decimal value. This will allow fractional values to be expressed. The scale value starts at zero, indicating that the file size is initially held in bytes. After the first iteration of the loop, should there be one, the filesize variable will be reduced by a factor of 1,024 whilst the scale will be increased to one, indicating a value in kilobytes.

The rounding in the loop definition checks that the file size is greater than or equal to one thousand after rounding. If, for example, the file size were 999.9, the loop would continue. The rounding is essential, as later in the algorithm the value will be formatted to achieve three significant digits. We do not want this formatting to round the value upwards to one thousand, which would use four significant digits.

Formatting the Scaled Value

On completion of the loop, we will have a file size that is between zero and 999 after rounding. This value can now be included in a string along with the scale size constant name. Firstly, the value must be rounded to three significant figures. This can be combined with the conversion to a string using the ToString method and a format specifier. In this case, the "f" format specifier will be used to indicate a fixed number of decimal places.

The number of decimal places to use depends upon the file size value. If the value is one hundred or greater, no decimal places will be included. For smaller values than this that are ten or larger, one decimal place is required. Finally, for values below ten, two decimal places will be included in the format specifier. The exception to this rule is when the scale is set to bytes. In this case, we do not want to show bytes with decimal places so the number is formatted without any.

The code uses several if statements to determine the required formatting before adding the enumeration value using string.Format. The resultant string is then returned. To complete the method, add the following code:

string formatted;

if (filesize >= 100 || scale == SizeScales.bytes)
    formatted = filesize.ToString("f0");
else if (filesize >= 10)
    formatted = filesize.ToString("f1");
else
    formatted = filesize.ToString("f2");

return string.Format("{0} {1}", formatted, scale);

Testing the Code

You can now test the method by calling it, passing an unsigned integer value, and reviewing the results. For a Windows forms-based demonstration of the method, download the source code and demo using the link at the top of this article.

23 August 2008