GithubHelp home page GithubHelp logo

aldaviva / datasizeunits Goto Github PK

View Code? Open in Web Editor NEW
13.0 3.0 0.0 58 KB

๐Ÿ“ Convert and format data size units in .NET (bits, bytes, kilobits, kilobytes, and others).

Home Page: https://www.nuget.org/packages/DataSizeUnits

License: Apache License 2.0

C# 100.00%
bytes data-size megabytes gigabytes

datasizeunits's Introduction

DataSizeUnits

NuGet Package Build status Test status Coverage status

Convert and format data size units in .NET (bits, bytes, kilobits, kilobytes, and others).

Features

  • Convert between many units of digital information, including bits, bytes, and their higher-order units (kilobits and kilobytes and the rest, up to and including exabits and exabytes)

    • 150 Mbit โ†’ 17.8 MByte
      DataSize sizeInMegabytes = new DataSize(150, Unit.Megabit).ConvertToUnit(Unit.Megabyte);
      // sizeInMegabytes.Quantity == 17.8
      // sizeInMegabytes.Unit == Unit.Megabyte
  • Normalize a number of bytes to an automatically-selected unit based on its magnitude

    • 2,097,152 bytes โ†’ 2 MB
      DataSize normalized = new DataSize(2_097_152).Normalize();
      // normalized.Quantity == 2.0
      // normalized.Unit == Unit.Megabyte
    • 2,097,152 bytes โ†’ 16.78 mbit
      DataSize normalized = new DataSize(2_097_152).Normalize(true); // pass true to get bits units instead of bytes
      // normalized.Quantity == 16.78
      // normalized.Unit == Unit.Megabit
    • The unit will be automatically selected so the value is greater than or equal to 1 of that unit, and less than 1 of the next largest unit. For example, 2,097,152 bytes is greater than or equal to 1 MB and less than 1 GB, so it is normalized to MB.
  • Parse and format unit names and abbreviations

    • Megabyte, MByte, mebibyte, MiB, MB, and M are all megabytes
      Unit unit = DataSize.ParseUnit("MB");
      // unit == Unit.Megabyte
    • Abbreviations for each unit are of the short, case-sensitive forms.
      string abbreviation = Unit.Terabyte.ToAbbreviation();
      // abbreviation == "TB"
      string iecAbbreviation = Unit.Terabyte.ToAbbreviation(true); // pass true for the IEC abbreviations (kibibyte, etc.)
      // iecAbbreviation == "TiB";
    • Get the unit names in JEDEC (TB) or IEC (TiB) variants.
      string name = Unit.Terabyte.ToName();
      // name == "terabyte";
      string iecName = Unit.Terabyte.ToName(true); // pass true for the IEC names (kibibyte, etc.)
      // iecName == "tebibyte";
  • Format bytes as a string with different unit and precision options

    • 1,536 bytes to kilobytes, 1 digit after the decimal point โ†’ 1.5 KB
      string formatted = new DataSize(1536).ConvertToUnit(Unit.Kilobyte).ToString(1); // precision 1, don't change units
      // formatted == "1.5 KB"
      string formatted = new DataSize(1536).ToString(1, Unit.Kilobyte); // precision 1, change to specified unit
      // formatted == "1.5 KB"
      string formatted = new DataSize(1536).ToString("KB1"); // precision 1, change to specified unit
      // formatted == "1.5 KB"
      string formatted = new DataSize(1536).ToString(1, true); // precision 1, normalize to automatic unit
      // formatted == "1.5 KB"
      string formatted = string.Format(new DataSizeFormatter(), "Size: {0:KB1}", 1536); // precision 1, change to specified unit
      // formatted == "Size: 1.5 KB"
      string formatted = string.Format(new DataSizeFormatter(), "Size: {0:A1}", 1536); // precision 1, normalize to automatic unit
      // formatted == "Size: 1.5 KB"
    • The format specifier (like KB1 above) is made up of two optional parts, the destination unit (KB) and the precision (1).
    • The destination unit (KB) is the data size unit to which you want the input bytes to be converted. You can also specify A to automatically normalize the unit of bytes and higher magnitudes, which is the default behavior if you omit the destination unit, and a normalizes to bits. Case matters for ambiguous units, like kB/KB/K for kilobytes and kb/k/Kb for kilobits. Unambiguous units like kilobyte/kbyte/kibibyte/kib can be provided in any case.
    • The precision (1) is the number of digits after the decimal place. If you omit this, it will use the default number value for the culture of the current thread, for example 2. Set this to 0 if you want integers only.

Install

This package is available in NuGet Gallery.

This is a .NET Standard 2.0 library, and can be depended upon in projects with .NET Framework 4.6.1, .NET Core 2.0, or later.

Tool Installation
Visual Studio NuGet Package Manager Search for DataSizeUnits
Visual Studio Package Manager Console Install-Package DataSizeUnits
.NET Core SDK CLI dotnet add package DataSizeUnits

datasizeunits's People

Contributors

aldaviva avatar dependabot[bot] avatar

Stargazers

 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.