Code formatter for Windows Live Writer

I stumbled across a great code formatter for Windows Live Writer today. Here’s an example, using a C# function that converts a number into a formatted file size:

       public static string SizeToString(long size)
        {
            const long kilobyte = 1L << 10;
            const long megabyte = 1L << 20;
            const long gigabyte = 1L << 30;
            const long terabyte = 1L << 40;
            string kbSuffix = "KB";
            string mbSuffix = "MB";
            string gbSuffix = "GB";
            string tbSuffix = "TB";
            string suffix = kbSuffix; 

            double divisor = kilobyte;//KB
            if (size > 0.9 * terabyte)
            {
                divisor = terabyte;
                suffix = tbSuffix;
            }
            else if (size > 0.9 * gigabyte)
            {
                divisor = gigabyte;
                suffix = gbSuffix;
            }
            else if (size > 0.9 * megabyte)
            {
                divisor = megabyte;
                suffix = mbSuffix;
            } 

            double newSize = size / divisor;
            return string.Format("{0:F2}{1}", newSize,suffix);
        }

Popularity: 2% [?]

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>