0

I have a project in ASP.Net MVC 5. I want to use German decimal format in overall software. I set globalization "de-DE" in web config and I have overridden "BeginExecuteCore" in Base Controller which all of Controllers inherit.

<globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="de-DE" uiCulture="de-DE"/>


protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
    {
        CultureInfo culture = new CultureInfo("de-DE", true);
        culture.NumberFormat.NumberDecimalSeparator = ",";
        Thread.CurrentThread.CurrentCulture = culture;
        Thread.CurrentThread.CurrentUICulture = culture;
        return base.BeginExecuteCore(callback, state);
    }

Also, I changed regional format to "German" in windows. It worked when I pass decimal to controller. Actually software allowed decimal only with comma and does not allow with dot. But when I fetch decimal using EF from database, that fetched with Dot and loaded in UI with Dot. I need to display comma in all decimal number in overall software. What's wrong?

Fildor
  • 14,510
  • 4
  • 35
  • 67

2 Answers2

0

EF is not your problem. Culture is invoked when you execute ToString() on decimal. You changed the culture in your controller, but you need to change it in your view also. Try putting the following code in your Views/Shared/_Layout.cshtml

@{ 
    System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("de-DE", true);
    culture.NumberFormat.NumberDecimalSeparator = ",";
    System.Threading.Thread.CurrentThread.CurrentCulture = culture;
    System.Threading.Thread.CurrentThread.CurrentUICulture = culture;
}

For AJAX, you would have to convert the number to string before putting it on ui, so you could use x.toLocaleString('de-DF');

mZm
  • 136
  • 7
  • In database, there are all decimal numbers with comma. But when i put the breakpoint on LINQ code, I see that the decimal numbers are fetched with dot. – Vahid Borandeh Dec 03 '19 at 10:15
  • decimal numbers are decimal numbers. comma or dot is put when you do .ToString() on the numbers. Your debug shows dot, probably because your visual studio is set to decimal dot... Try to put .ToString() in debug on the decimal numbers – mZm Dec 03 '19 at 10:22
  • When i call "ToString" in C# on decimal field, that return with comma. But i used AJAX and JQuery and when i call toString in Jquery, it return with dot. – Vahid Borandeh Dec 03 '19 at 10:47
  • Then you have 2 options. 1st is to return strings instead of decimal in the action that the ajax calls. 2nd is to convert to string in javascript like in https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript – mZm Dec 03 '19 at 10:54
  • Sorry I copied the wrong link for javascript convert to string, I edited it above – mZm Dec 03 '19 at 10:58
0

I found a solution. If I use toLocaleString("de") (for German Language) in JQuery code throughout the project, the problem will be resolved. But I'm looking for a solution to be able to Multilingualize project and change the comma by just changing one parameter throughout the project.