Set CultureInfo in Asp.net Core to have a . as CurrencyDecimalSeparator instead of ,
This is what solves it for me:
Setting the following in StartUp.Configure
var cultureInfo = new CultureInfo("en-US");
cultureInfo.NumberFormat.CurrencySymbol = "€";
CultureInfo.DefaultThreadCurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
.NET Core localization globalization
you can use bellow code
Startup.ConfigureServices
CultureInfo[] supportedCultures = new[]
{
new CultureInfo("ar"),
new CultureInfo("fa"),
new CultureInfo("en")
};
services.Configure<RequestLocalizationOptions>(options =>
{
options.DefaultRequestCulture = new RequestCulture("ar");
options.SupportedCultures = supportedCultures;
options.SupportedUICultures = supportedCultures;
options.RequestCultureProviders = new List<IRequestCultureProvider>
{
new QueryStringRequestCultureProvider(),
new CookieRequestCultureProvider()
};
});
Startup.Configure
app.UseRequestLocalization();
change language:
[HttpPost]
public IActionResult SetLanguage(string culture, string returnUrl)
{
Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
);
return LocalRedirect(returnUrl);
}
More Details: here
Trying to set the decimal separator for the current language, getting Instance is read Only
You need to create a new culture and you can use the current culture as a template and only change the separator.
Then you must set the current culture to your newly created one as you cannot change the property within current culture directly.
string CultureName = Thread.CurrentThread.CurrentCulture.Name;
CultureInfo ci = new CultureInfo(CultureName);
if (ci.NumberFormat.NumberDecimalSeparator != ".")
{
// Forcing use of decimal separator for numerical values
ci.NumberFormat.NumberDecimalSeparator = ".";
Thread.CurrentThread.CurrentCulture = ci;
}
Using NumberFormat property in ASP.NET Core
You can add UseRequestLocalization
to the Configure()
method in Startup.cs:
public void Configure(IApplicationBuilder app)
{
app.UseIISPlatformHandler();
app.UseRequestLocalization(new RequestCulture(new CultureInfo("es")));
app.Run(async (context) =>
{
context.Response.ContentType = "text/html";
await context.Response.WriteAsync(HtmlEncoder.Default.HtmlEncode(1000.5f.ToString("C")));
});
}
Result:
1.000,50 €
Also, more to the point of the original question:
public void Configure(IApplicationBuilder app)
{
app.UseIISPlatformHandler();
var modified = new CultureInfo(CultureInfo.DefaultThreadCurrentCulture.DisplayName);
modified.NumberFormat.CurrencySymbol = "RM";
modified.NumberFormat.CurrencyDecimalDigits = 2;
modified.NumberFormat.CurrencyDecimalSeparator = ".";
modified.NumberFormat.CurrencyGroupSeparator = ",";
app.UseRequestLocalization(new RequestCulture(modified));
app.Run(async (context) =>
{
context.Response.ContentType = "text/html";
await context.Response.WriteAsync(HtmlEncoder.Default.HtmlEncode(1000.5f.ToString("C")));
});
}
Result:
RM1,000.50
Related Topics
How Does Task<Int> Become an Int
Why Is Try {...} Finally {...} Good; Try {...} Catch{} Bad
Can You Explain Liskov Substitution Principle with a Good C# Example
How to Store Data Locally in .Net (C#)
Spawn Multiple Threads for Work Then Wait Until All Finished
How to Convert Securestring to System.String
Handling the Window Closing Event with Wpf/Mvvm Light Toolkit
Routing with Multiple Get Methods in ASP.NET Web API
Foreach Loop, Determine Which Is the Last Iteration of the Loop
Difference Between Lookup() and Dictionary(Of List())
Creating PDF Files at Runtime in C#
When Should I Use the Hashset<T> Type
The Process Cannot Access the File Because It Is Being Used by Another Process