C
C#2y ago
Lamine

❔ Number formatting issue

Hi all! I'm currently dealing with an issue where number formatting is not working as intended. I'm using ASP.NET MVC and when I try to submit I form, form validation stops the request and it shows " The field ... must be a number". Whenever I change the numbers to for example 4.80, it submits the form, however it saves it as 480, so it doesn't include the comma correctly. We've recently added Globalization/localization to our application and this seems to have caused the issue. Code snippet of that:
// Configure localization
var supportedCultures = new[] { "nl", "de" };
var localizationOptions = new RequestLocalizationOptions()
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures);


app.UseRequestLocalization(localizationOptions);
// Configure localization
var supportedCultures = new[] { "nl", "de" };
var localizationOptions = new RequestLocalizationOptions()
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures);


app.UseRequestLocalization(localizationOptions);
I've previously solved the issue by forcing the culture, like this:
// Set culture to
var ci = new CultureInfo("nl-NL")
{
DateTimeFormat =
{
DateSeparator = "-"
},
NumberFormat = NumberFormatInfo.InvariantInfo
};
CultureInfo.DefaultThreadCurrentCulture = ci;
CultureInfo.DefaultThreadCurrentUICulture = ci;
// Set culture to
var ci = new CultureInfo("nl-NL")
{
DateTimeFormat =
{
DateSeparator = "-"
},
NumberFormat = NumberFormatInfo.InvariantInfo
};
CultureInfo.DefaultThreadCurrentCulture = ci;
CultureInfo.DefaultThreadCurrentUICulture = ci;
However, right now I'm not able to do that, as it makes use of the app.UseRequestLocalization(localizationOptions);. How would I force the culture to make use of a certain number seperator and ensure that it saves it correctly and not as a whole number?
7 Replies
Lamine
Lamine2y ago
Just to give some more info, the property that is bound to all the numbers is actually a decimal. I found out that this is best for saving things like prices. By using a decimal, I've also managed to solve the issues when globalization and localization wasn't installed yet.
Anton
Anton2y ago
$code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
Lamine
Lamine2y ago
Thanks
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Lamine
Lamine2y ago
No, the issue was not resolved yet.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.