budul.ConverterExtensions
2.0.0
dotnet add package budul.ConverterExtensions --version 2.0.0
NuGet\Install-Package budul.ConverterExtensions -Version 2.0.0
<PackageReference Include="budul.ConverterExtensions" Version="2.0.0" />
<PackageVersion Include="budul.ConverterExtensions" Version="2.0.0" />
<PackageReference Include="budul.ConverterExtensions" />
paket add budul.ConverterExtensions --version 2.0.0
#r "nuget: budul.ConverterExtensions, 2.0.0"
#:package budul.ConverterExtensions@2.0.0
#addin nuget:?package=budul.ConverterExtensions&version=2.0.0
#tool nuget:?package=budul.ConverterExtensions&version=2.0.0
ConverterExtensions
Extension methods to convert data, mainly to extract numbers from messy strings and to parse enums.
Installation
dotnet add package budul.ConverterExtensions
Requires .NET 10 (net10.0). Version 2.0.0 dropped all netstandard and netcoreapp targets. Use 1.x if you need them.
Usage
All methods live in the namespace ConverterExtensions.
Numbers from strings
The first number found in the string is extracted and parsed, surrounding text is ignored.
using System.Globalization;
using ConverterExtensions;
var de = CultureInfo.GetCultureInfo("de-DE");
var en = CultureInfo.GetCultureInfo("en-GB");
"AAa-9.999,99bb".ToDouble(provider: de); // -9999.99
"AAa9,999.99bb".ToDecimal(provider: en); // 9999.99
"Gleis 12b".ToInt(); // 12
"abc".ToNullableInt(); // null
"abc".ToInt(); // 0
| Method | Returns | If nothing is parsed |
|---|---|---|
ToInt, ToLong, ToDouble, ToDecimal |
value type | 0 |
ToNullableInt, ToNullableLong, ToNullableDouble, ToNullableDecimal |
nullable value type | null |
All methods take optional NumberStyles style (default NumberStyles.Number) and IFormatProvider provider parameters.
Things to be aware of:
- Without a
provider,CultureInfo.InvariantCultureis used, not the current culture. - The non-nullable methods (
ToInt,ToLong,ToDouble,ToDecimal) return0if parsing fails. A failed parse cannot be distinguished from a real0, use theToNullable...methods if you need that. - Integer methods stop at the first separator:
"1.234".ToInt(provider: de)is1, not1234. UseToDecimalorToLongafter removing separators if you need the latter. - A leading
-directly before the digits is taken as sign ("Gleis-5"gives-5). - Only ASCII digits (
0-9) are recognized. Exponents (1e5) are not supported. - Out-of-range values (for example
"99999999999".ToInt()) count as "nothing parsed".
Generic conversion
"42".Convert<string, int>(); // 42
"5".Convert<string, int?>(); // 5
" ".Convert<string, int>(); // 0 (blank string)
0.Convert<int, string>(); // "0"
Convert<T, U> wraps System.Convert.ChangeType and uses the invariant culture. null and blank strings result in default(U).
Enums
public enum State { None, Active }
"active".ToEnum<State>(); // State.Active (case-insensitive)
"unknown".ToEnum<State>(); // State.None (default value)
Changes in 2.0.0
- Target framework is
net10.0only. ToEnum<T>is constrained tostruct, Enuminstead ofstruct, IConvertible, the runtime check was removed.Convert<T, U>no longer short-circuits ondefault(T)for value types, so0.Convert<int, string>()returns"0"instead ofnull.Convert<T, U>usesCultureInfo.InvariantCultureinstead of the current culture, consistent with the number extensions.- Number extraction uses source-generated regexes and does not allocate substrings.
Build and test
dotnet build
dotnet test
The tests use xUnit.net v3. The culture-specific tests need ICU culture data, so InvariantGlobalization must not be enabled (relevant for minimal Linux containers).
License
MIT, see LICENSE.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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. |
-
net10.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.