Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thomaslevesque/humanbytes
A library to convert byte sizes to a human readable form
https://github.com/thomaslevesque/humanbytes
bytes csharp dotnet human size
Last synced: 3 months ago
JSON representation
A library to convert byte sizes to a human readable form
- Host: GitHub
- URL: https://github.com/thomaslevesque/humanbytes
- Owner: thomaslevesque
- License: apache-2.0
- Created: 2014-08-09T02:11:56.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2022-09-14T03:19:03.000Z (over 2 years ago)
- Last Synced: 2024-10-31T22:50:22.892Z (3 months ago)
- Topics: bytes, csharp, dotnet, human, size
- Language: C#
- Size: 37.1 KB
- Stars: 32
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# HumanBytes
[![NuGet version](https://img.shields.io/nuget/v/HumanBytes.svg?logo=nuget)](https://www.nuget.org/packages/HumanBytes)
[![AppVeyor build](https://img.shields.io/appveyor/ci/thomaslevesque/humanbytes.svg?logo=appveyor)](https://ci.appveyor.com/project/thomaslevesque/humanbytes)
[![AppVeyor tests](https://img.shields.io/appveyor/tests/thomaslevesque/humanbytes.svg?logo=appveyor)](https://ci.appveyor.com/project/thomaslevesque/humanbytes/build/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:
```csharp
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:
```csharp
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.