A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/thomaslevesque/HumanBytes below:

thomaslevesque/HumanBytes: A library to convert byte sizes to a human readable form

A library to convert byte sizes to a human readable form.

Install the library via NuGet

PM> Install-Package HumanBytes

The Bytes extension method returns an object that has a human readable string representation:

var f = new FileInfo("TheFile.jpg");
Console.WriteLine($"The size of '{f.Name}' is {f.Length.Bytes()}");
// Prints "The size of 'TheFile.jpg' is 89 KB"

The value is formatted using the default formatter; you can change the default formatter settings through the ByteSizeFormatter.Default static property.

If you need more control, create an instance of ByteSizeFormatter and change its settings:

var formatter = new ByteSizeFormatter
{
    Convention = ByteSizeConvention.Binary,
    DecimalPlaces = 1,
    NumberFormat = "#,##0.###",
    MinUnit = ByteSizeUnit.Kilobyte,
    MaxUnit = ByteSizeUnit.Gigabyte,
    RoundingRule = ByteSizeRounding.Closest,
    UseFullWordForBytes = true,
};

var f = new FileInfo("TheFile.jpg");
Console.WriteLine($"The size of '{f.Name}' is {formatter.Format(f.Length)}");
// Prints "The size of 'TheFile.jpg' is 88.7 KiB"

At this stage, HumanBytes only supports English and French. However, if you need support for another language, it's very easy, as there are only 3 terms that need to be translated. Just create an instance of ByteSizeFormatter as shown above, and set the following properties:

The values set on these properties will be used instead of the ones defined in resources.

If you would like to add permanent support for another language, you'll have to modify the library itself. Just clone the project, add a Resources.xx.resx file (where xx is the language code) in the HumanBytes/Properties folder, translate the terms, and build.

I'll be glad to accept pull requests to support more languages.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4