DataSizeUnits 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package DataSizeUnits --version 1.0.0                
NuGet\Install-Package DataSizeUnits -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DataSizeUnits" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DataSizeUnits --version 1.0.0                
#r "nuget: DataSizeUnits, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DataSizeUnits as a Cake Addin
#addin nuget:?package=DataSizeUnits&version=1.0.0

// Install DataSizeUnits as a Cake Tool
#tool nuget:?package=DataSizeUnits&version=1.0.0                

DataSizeUnits

Deal with data size units in C# (bytes, kilobytes, megabytes, and others).

Features

  • Convert a number of bytes to different units (kilobytes, megabytes, gigabytes, terabytes, petabytes)
    • 2,097,152 bytes to kilobytes → 2048 KB
      double scaled = DataSize.convert(2_097_152, Unit.KILOBYTE);
      // scaled == 2048
      
  • Convert a number of bytes to an automatically-selected unit based on its size
    • 2,097,152 bytes → 2 MB
      (double scaled, Unit unit) = DataSize.convert(2_097_152);
      // scaled == 2
      // unit == Unit.MEGABYTE
      
    • 2,097,152 bytes → 16.78 mbit
      (double scaled, Unit unit) = DataSize.convert(2_097_152, false);
      // scaled == 16.78
      // 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 converted to MB.
  • Convert between any units, including any combination of bits, bytes, and their higher-order units (kilobits and kilobytes and the rest, up to and including petabits and petabytes)
    • 150 Mbit → 17.8 MByte
      double scaled = DataSize.convert(150, Unit.MEGABIT, Unit.MEGABYTE);
      // scaled == 17.8
      
  • Interpret different suffices
    • Megabyte, MByte, mebibit, mib, mibit, MB, and M are all megabytes
      Unit unit = DataSize.forAbbreviation("mebibit");
      // unit == Unit.MEGABYTE
      
    • Default suffices for each unit are of the short, case-sensitive forms.
      string abbreviation = DataSize.toAbbreviation(Unit.TERABYTE);
      // abbreviation == "TB"
      
  • Format bytes as a string with different unit and precision options
    • {1536:KB1}1.5 KB
      string formatted = string.Format(DataSize.FORMATTER, "Size: {0:KB1}", 1536);
      // formatted == "Size: 1.5 KB"
      
    • The format specifier (KB1 above) is made up of two optional parts, the destination unit (KB) and the precision (1).
    • The destination unit is the data size unit to which you want the input bytes to be converted. You can pass any data size unit abbreviation. Case matters for ambiguous units, like kB/KB/K for kilobytes and kb/k/Kb for kilobits. Unambiguous units like kilobyte/kbyte/kibibit/kib/kibit can be provided in any case. You can also specify A to automatically select the unit of bytes and higher magnitudes, and likewise a for bits, which is the default behavior if you omit the destination unit.
    • The precision is the number of digits after the decimal place. If you omit this, it will use the default number format 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 the 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
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.0-beta1 102 10/1/2024
2.1.0 58,753 7/11/2021
2.0.0 1,650 1/19/2020
1.0.0 1,102 10/27/2019
0.0.1 1,469 10/27/2019