Code Monkey home page Code Monkey logo

humanbytes's Introduction

HumanBytes

NuGet version AppVeyor build AppVeyor tests

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

Installation

Install the library via NuGet

PM> Install-Package HumanBytes

Basic usage

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.

More advanced usage

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"

Localization

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:

  • ByteSymbol: the symbol for byte, e.g. "B" in English.
  • ByteWord: the word for byte, e.g. "byte" in English.
  • BytesWord: the word for several bytes, e.g. "bytes" in English.

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.

humanbytes's People

Contributors

thomaslevesque avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.