AspDotNetCore2BinderFloatingDecimalModel 1.0.0
dotnet add package AspDotNetCore2BinderFloatingDecimalModel --version 1.0.0
NuGet\Install-Package AspDotNetCore2BinderFloatingDecimalModel -Version 1.0.0
<PackageReference Include="AspDotNetCore2BinderFloatingDecimalModel" Version="1.0.0" />
<PackageVersion Include="AspDotNetCore2BinderFloatingDecimalModel" Version="1.0.0" />
<PackageReference Include="AspDotNetCore2BinderFloatingDecimalModel" />
paket add AspDotNetCore2BinderFloatingDecimalModel --version 1.0.0
#r "nuget: AspDotNetCore2BinderFloatingDecimalModel, 1.0.0"
#:package AspDotNetCore2BinderFloatingDecimalModel@1.0.0
#addin nuget:?package=AspDotNetCore2BinderFloatingDecimalModel&version=1.0.0
#tool nuget:?package=AspDotNetCore2BinderFloatingDecimalModel&version=1.0.0
ASP.NET Core 3.0 BinderFloatingDecimalModel
Model Binder in ASP.NET Core 3.0 for Floating Point Numeric Values [double, float или decimal]
Use in your project:
using AspDotNetCore2BinderFloatingDecimalModel;
// ...
public class Startup
{
// ...
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddMvc(opts =>
{
opts.ModelBinderProviders.Insert(0, new CustomDecimalModelBinderProvider());
}).SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
}
}
A well-known feature of the standard model binding in ASP.NET Core 3.0 is that it is impossible to fully use floating point value types in properties. The built-in model binding tries to process all numeric values as integers, even if the type of the model property is declared as a floating-point number. From here it becomes problematic to use models with properties of types [double, float or decimal] in forms.
This model binding not only fixes this problem, but also frees the user from restricting the use of a fractional separator (period or comma). This binder allows you to use any fraction separator. Any separator is forcibly replaced with a system one before conversion.
Regex FloatSeparator = new Regex(@"[.,]", RegexOptions.Compiled);
////////////////////////////////////////////////////
// replace the fractional separator with the current system
FieldValueAsNormalString = FloatSeparator.Replace(FieldValueAsNormalString, CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
In addition, a truncated format for recording numbers is allowed. For example: 0.5 and .5 will give one result. As well as 5.0 == 5. This feature looks ambiguous and controversial ⇒ you can comment out this "value processing".
if (FloatSeparator.IsMatch(FieldValueAsString))
FieldValueAsNormalString = "0" + FieldValueAsNormalString + "0";
Special attention is required to the regular expression pattern of the intended value. File: ** CustomFloatingModelBinder.cs **
protected readonly Regex FloatPattern = new Regex(@"^(-?)[0-9]*(?:[.,][0-9]*)?$", RegexOptions.Compiled);
Check it to meet the tasks in your context
| 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 was computed. 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 | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- Microsoft.AspNetCore.Mvc.Core (>= 2.2.5)
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 |
|---|---|---|
| 1.0.0 | 694 | 3/10/2020 |