GMana.Utils
0.0.10
See the version list below for details.
dotnet add package GMana.Utils --version 0.0.10
NuGet\Install-Package GMana.Utils -Version 0.0.10
<PackageReference Include="GMana.Utils" Version="0.0.10" />
<PackageVersion Include="GMana.Utils" Version="0.0.10" />
<PackageReference Include="GMana.Utils" />
paket add GMana.Utils --version 0.0.10
#r "nuget: GMana.Utils, 0.0.10"
#:package GMana.Utils@0.0.10
#addin nuget:?package=GMana.Utils&version=0.0.10
#tool nuget:?package=GMana.Utils&version=0.0.10
GMana.Utils
A collection of utility extensions, helpers, and services for .NET applications, designed to simplify common tasks such as IP address retrieval, number-to-words conversion, byte size formatting, and more.
Extensions
Khmer Language Helpers
IsKhmer
: Checks if a string contains only Khmer characters.ContainsKhmer
: Checks if a string contains any Khmer characters.
ToByteSizeExtensions
Converts numeric values (string, decimal, double, long) into human-readable byte sizes (e.g., KB, MB, GB).
using GMana.Utils.Extensions;
// String usage
string bytesStr = "5242880";
Console.WriteLine(bytesStr.ToByteSizeString()); // "5.00 MB"
Console.WriteLine(bytesStr.ToByteSizeString("N0")); // "5 MB"
// Decimal usage
decimal bytesDec = 1073741824m;
Console.WriteLine(bytesDec.ToByteSizeString()); // "1.00 GB"
// Double usage
double bytesDbl = 1099511627776.0;
Console.WriteLine(bytesDbl.ToByteSizeString()); // "1.00 TB"
// Long usage
long bytesLong = 123456789;
Console.WriteLine(bytesLong.ToByteSizeString()); // "117.74 MB"
ToRelativeTimeString
Converts a DateTime to a human-readable relative time string (e.g., "5 seconds ago").
var pastTime = DateTime.Now.AddSeconds(-30);
string result = pastTime.ToRelativeTimeString(); // "30 seconds ago"
Unit Test Example
[Theory]
[InlineData(1, "1 second ago")]
[InlineData(30, "30 seconds ago")]
[InlineData(59, "59 seconds ago")]
public void ToRelativeTimeString_SecondsAgo_ReturnsSecondsAgoText(int seconds, string expected)
{
var pastTime = DateTime.Now.AddSeconds(-seconds);
var result = pastTime.ToRelativeTimeString();
Assert.Equal(expected, result);
}
NumericExtensions
Formats large numbers into compact strings with suffixes (e.g., "1.5k", "2.3M").
// Default precision (2 decimal places)
int number = 1456;
string formatted = number.ToRoundedString(); // "1.46k"
// Custom precision
formatted = number.ToRoundedString(1); // "1.5k"
formatted = number.ToRoundedString(0); // "1k"
// Larger numbers
number = 1500000;
formatted = number.ToRoundedString(); // "1.50M"
// Double values
double value = 1346.75;
formatted = value.ToRoundedString(); // "1.35k"
GetIPAddress
Retrieves the IP address of the current machine.
using GMana.Utils;
var ip = IP.GetIPAddress(); // Returns the IP address as a string
ClientIpService
A service to retrieve the client’s IP address in Blazor applications.
Setup for Blazor Server
// Program.cs
builder.Services.AddHttpContextAccessor();
builder.Services.AddScoped<IClientIpService, ClientIpService>();
Setup for Blazor WebAssembly
// Program.cs
builder.Services.AddScoped<IClientIpService, ClientIpService>();
Usage in a Component
@inject IClientIpService IpService
<p>Client IP: @clientIp</p>
@code {
private string clientIp = "Loading...";
protected override async Task OnInitializedAsync()
{
clientIp = await IpService.GetClientIpAsync();
}
}
JavaScript for WebAssembly
Add this script to your WebAssembly project:
<script src="_content/BlazorIpAddress/js/ipfetcher.js"></script>
NumberToWordsConverter
Converts a decimal number to English words (e.g., for currency).
decimal number = 165.93m;
string result = NumberToWordsConverter.ConvertToWords(number);
Console.WriteLine(result); // "One Hundred Sixty-Five Dollar and Ninety-Three Cents"
NumberToKhmerWordsConverter
Converts a decimal number to Khmer words with a custom currency unit.
decimal number = 165.93m;
// Using Dollar (ដុល្លារ)
string dollarResult = NumberToKhmerWordsConverter.ConvertToKhmerWords(number, "ដុល្លារ");
Console.WriteLine(dollarResult); // "មួយរយហុកសិបប្រាំ ដុល្លារ និង កៅសិបបី សេន"
// Using Riel (រៀល)
string rielResult = NumberToKhmerWordsConverter.ConvertToKhmerWords(number, "រៀល");
Console.WriteLine(rielResult); // "មួយរយហុកសិបប្រាំ រៀល"
Stringify
// Test Case
[Theory]
[InlineData(null, "")]
[InlineData("", "")]
[InlineData(" ", "")]
[InlineData("Example.txt", "example.txt")]
[InlineData("My-File_Name.DOC", "my_file_name.doc")]
[InlineData("Complex@File#Name!", "complex_file_name")]
[InlineData("NoExtensionFile", "no_extension_file")]
[InlineData("123-456_file.TEST", "123_456_file.test")]
[InlineData("file.with.multiple.dots.TXT", "file_with_multiple_dots.txt")]
[InlineData("UPPERCASE.JPG", "uppercase.jpg")]
[InlineData("Special--_Chars!.mp3", "special_chars.mp3")]
GetDaySuffix
// Test Case
[Theory]
[InlineData(1, "1st")]
[InlineData(2, "2nd")]
[InlineData(3, "3rd")]
[InlineData(4, "4th")]
[InlineData(11, "11th")]
[InlineData(12, "12th")]
[InlineData(13, "13th")]
[InlineData(21, "21st")]
[InlineData(22, "22nd")]
[InlineData(23, "23rd")]
[InlineData(24, "24th")]
[InlineData(101, "101st")]
[InlineData(111, "111th")]
[InlineData(112, "112th")]
[InlineData(113, "113th")]
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. 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. |
-
net9.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.3.0)
- Microsoft.JSInterop (>= 9.0.3)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on GMana.Utils:
Package | Downloads |
---|---|
GMana.Extensions
The GMana.Extensions is a .NET library that provides a collection of extension methods to enhance functionality for common .NET types, including string manipulation, numeric formatting, JSON handling, dictionary operations, enum utilities, character validation, and byte size formatting. Features specialized Khmer language support. |
GitHub repositories
This package is not used by any popular GitHub repositories.