DataSizeUnits 4.0.0-beta.1
See the version list below for details.
dotnet add package DataSizeUnits --version 4.0.0-beta.1
NuGet\Install-Package DataSizeUnits -Version 4.0.0-beta.1
<PackageReference Include="DataSizeUnits" Version="4.0.0-beta.1" />
<PackageVersion Include="DataSizeUnits" Version="4.0.0-beta.1" />
<PackageReference Include="DataSizeUnits" />
paket add DataSizeUnits --version 4.0.0-beta.1
#r "nuget: DataSizeUnits, 4.0.0-beta.1"
#:package DataSizeUnits@4.0.0-beta.1
#addin nuget:?package=DataSizeUnits&version=4.0.0-beta.1&prerelease
#tool nuget:?package=DataSizeUnits&version=4.0.0-beta.1&prerelease
πΎπΏ DataSizeUnits
Convert and format data size units (bits, bytes, kilobits, kilobytes, and others).
Installation
This is a .NET Standard 2.0 library, and can be depended upon in projects which target at least .NET 5, .NET Core 2.0, or .NET Framework 4.6.1.
dotnet add package DataSizeUnits
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 quettabits and quettabytes)
- 150 Mbit β 17.8 MByte
double sizeInMegabytes = new DataSize(150, DataSizeUnit.Megabit).AsUnit(DataSizeUnit.Megabyte); // sizeInMegabytes == 17.8
- 150 Mbit β 17.8 MByte
Normalize a number of bytes to an automatically-selected unit based on its magnitude
- 2,097,152 bytes β 2 MB
(double quantity, DataSizeUnit unit) normalized = new DataSize(2_097_152).AsAutomaticUnit(); // normalized.quantity == 2.0 // normalized.unit == DataSizeUnit.Megabyte - 2,097,152 bytes β 16.78 mbit
normalized = new DataSize(2_097_152).AsAutomaticUnit(true); // pass true to get bits units instead of bytes // normalized.quantity == 16.78 // normalized.unit == DataSizeUnit.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.
- 2,097,152 bytes β 2 MB
Parse data sizes
- 1.5 MB β 1,572,864 bytes
DataSize parsed = DataSize.Parse("1.5 MB"); // parsed.Bytes == 1572864
- 1.5 MB β 1,572,864 bytes
Parse and format unit names and abbreviations
- Megabyte, MByte, mebibyte, MiB, MB, and M are all megabytes
DataSizeUnit? unit = DataSizeUnit.Parse("MB"); // unit == DataSizeUnit.Megabyte - Abbreviations for each unit are of the short, case-sensitive forms.
string abbreviation = DataSizeUnit.Terabyte.ToAbbreviation(); // abbreviation == "TB"string iecAbbreviation = DataSizeUnit.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 = DataSizeUnit.Terabyte.ToName(); // name == "terabyte"string iecName = DataSizeUnit.Terabyte.ToName(true); // pass true for the IEC names (kibibyte, etc.) // iecName == "tebibyte"
- Megabyte, MByte, mebibyte, MiB, MB, and M are all megabytes
Format bytes as a string with different unit and precision options
- Automatic precision from culture, automatic byte-based unit
string formatted = new DataSize(1572864).ToString(); // formatted == "1.50 MB" - Automatic precision, manual unit
formatted = new DataSize(1572864).ToString(DataSizeUnit.Kilobyte); // formatted == "1,536.00 kB" - Manual precision, automatic unit
formatted = new DataSize(1572864).ToString(1); // formatted == "1.5 MB" - Automatic precision, automatic bit-based unit
formatted = new DataSize(1572864).ToString(true); // formatted == "12.58 mb"
- Automatic precision from culture, automatic byte-based unit
Serialize and deserialize to and from bits
- JSON (System.Text.Json or Newtonsoft.Json)
{ "filename": "example.txt", "size": 8192 } - XML (XmlSerializer)
<?xml version="1.0" encoding="utf-8"?> <MyFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Filename>example.txt</Filename> <Size bits="8192" /> </MyFile>
- JSON (System.Text.Json or Newtonsoft.Json)
| Product | Versions 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 is compatible. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.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. |
-
.NETStandard 2.0
- System.Text.Json (>= 8.0.6)
-
net8.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 |
|---|---|---|
| 4.0.0-beta.2 | 24 | 3/13/2026 |
| 4.0.0-beta.1 | 32 | 3/12/2026 |
| 3.0.0 | 145 | 3/5/2026 |
| 3.0.0-beta2 | 473 | 6/5/2025 |
| 3.0.0-beta1 | 252 | 10/1/2024 |
| 2.1.0 | 109,348 | 7/11/2021 |
| 2.0.0 | 1,892 | 1/19/2020 |
| 1.0.0 | 1,374 | 10/27/2019 |
| 0.0.1 | 1,690 | 10/27/2019 |