DEV Community

Cover image for Readable File Sizes
Adam K Dean
Adam K Dean

Posted on

2

Readable File Sizes

In an effort to fill up this blog with code, I will over the next few weeks be looking back over my snippets, finding useful methods and posting them. Today's feature is a simple text formatter, which converts a long (representing bytes) into a string, giving you a nice readable file size.

So without further ado:

private static string GetReadableBytes(long lSize)
{
    double size = lSize;
    if (size >= 1024 && size < 1048576)
        return string.Format("{0:#,0.00} KB", (size / 1024));

    else if (size >= 1048576 && size < 1073741824)
        return string.Format("{0:#,0.00} MB", (size / 1048576));

    else if (size >= 1073741824 && size < 1099511627776)
        return string.Format("{0:#,0.00} GB", (size / 1073741824));

    else if (size >= 1099511627776)
        return string.Format("{0:#,0.00} TB", (size / 1099511627776));

    else return string.Format("{0} B", size);
}
Enter fullscreen mode Exit fullscreen mode

And here you can see the output.

/* Bytes                    Readable Size
 * =====                    =============
 * 436                  ->  436 B
 * 2159                 ->  2.11 KB
 * 36236                ->  35.39 KB
 * 2362893              ->  2.25 MB
 * 276227272            ->  263.43 MB
 * 62367298764          ->  58.08 GB
 * 3623647473473        ->  3.30 TB
 * 352635208736296      ->  320.72 TB
 * 236363536873629626   ->  214,971.38 TB
 **/
Enter fullscreen mode Exit fullscreen mode

Enjoy!

AWS Industries LIVE! Stream

Watch AWS Industries LIVE!

Discover how cloud technology is solving real-world problems on Industries LIVE!

Learn More

Top comments (0)

Image of Quadratic

Free AI chart generator

Upload data, describe your vision, and get Python-powered, AI-generated charts instantly.

Try Quadratic free

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay