budul.ConverterExtensions 2.0.0

dotnet add package budul.ConverterExtensions --version 2.0.0
                    
NuGet\Install-Package budul.ConverterExtensions -Version 2.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="budul.ConverterExtensions" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="budul.ConverterExtensions" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="budul.ConverterExtensions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add budul.ConverterExtensions --version 2.0.0
                    
#r "nuget: budul.ConverterExtensions, 2.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.
#:package budul.ConverterExtensions@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=budul.ConverterExtensions&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=budul.ConverterExtensions&version=2.0.0
                    
Install as a Cake Tool

ConverterExtensions

Extension methods to convert data, mainly to extract numbers from messy strings and to parse enums.

NuGet

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.InvariantCulture is used, not the current culture.
  • The non-nullable methods (ToInt, ToLong, ToDouble, ToDecimal) return 0 if parsing fails. A failed parse cannot be distinguished from a real 0, use the ToNullable... methods if you need that.
  • Integer methods stop at the first separator: "1.234".ToInt(provider: de) is 1, not 1234. Use ToDecimal or ToLong after 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.0 only.
  • ToEnum<T> is constrained to struct, Enum instead of struct, IConvertible, the runtime check was removed.
  • Convert<T, U> no longer short-circuits on default(T) for value types, so 0.Convert<int, string>() returns "0" instead of null.
  • Convert<T, U> uses CultureInfo.InvariantCulture instead 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.

Version Downloads Last Updated
2.0.0 93 11/29/2026
1.6.2 2,160 11/26/2021
1.6.1 3,475 11/26/2021
1.6.0 3,696 11/25/2021
1.5.0 719 10/11/2020
1.4.0 711 6/4/2020
1.3.0 689 5/5/2020
1.2.0 675 5/5/2020
1.1.0 671 5/5/2020
1.0.0 673 5/5/2020