locked
Convert.ToSingle("0,5", CultureInfo.CurrentUICulture) for culture "de-CH" works differently on my Dev. machines RRS feed

  • Question

  • User1883319856 posted

    Hi all

    I have two development machines, both have Windows 8.1 installed. On one of theme the operating System is installed in English on the other it is installed in German. On both machines I have set the Region in System Settings to "German (Switzerland)" and assuming the other language specific Settings, especially the one for the Decimal Symbol, would then be set identically (in this case to a comma).

    Now in my ASP.NET MVC 4 Application I set CurrentCulture and CurrentUICulture to "de-CH" in the Application_Start Event.

    When I then execute the following line of code it works on the machine where the os is installed in English, but doesn't work on the other machine:
    string attemptedValue = Convert.ToSingle("0,5", CultureInfo.CurrentUICulture); // Notice the comma in the string.

    Exception Message is like: "Input String was not in correct Format." When I chage the above line of code to:
    string attemptedValue = Convert.ToSingle("0.5", CultureInfo.CurrentUICulture); // Notice the dot in the string.

    then the opposite happens. It works on the machine where the os is in German installed, but doesn't work on the machine where the os is installend in English.

    So my question:
    How should I convert a string, representing an decimal value on the Server depending on the culture? I thought this is done in the way I try to implement it?

    Thanks for help
    Aedu

    Tuesday, January 13, 2015 3:09 AM

Answers

  • User753101303 posted

    Hi,

    Have you checked the value for CurrentCulture? IMO the point is that doing that in Application_Start (that happens once when the application starts) don't have any effect for later requests.

    If you want to always use de-CH just change the web.config file : https://msdn.microsoft.com/en-us/library/vstudio/hy4kkhe0(v=vs.100).aspx (and culture, possibly uiCulture)

    If you want to decide based on some condition the article still applies IMO. Keep in mind that a web server is not a process that just starts. It uses quite numerous threads etc... that can be reused over time for distinct requests etc... so IMO you can't do just that in Application_Start and expect to be done with it (unless perhaps using this new 4.5 stuff). I added the article you were referred list to my "exploration" list (but once again if you want a fixed culture rather than to select the culture based on some condition, just use the web.config file).

    Not given a close look but http://blogs.msdn.com/b/tmarq/archive/2007/07/21/asp-net-thread-usage-on-iis-7-0-and-6-0.aspx should give an idea about how a web server works.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 6, 2015 5:58 AM

All replies

  • User-166373564 posted

    Hi Aedu,

    We use Convert.ToSingle Method (String, IFormatProvider) to convert the specified string representation of a number to an equivalent single-precision floating-point number, using the specified culture-specific formatting information.

    We use CultureInfo.CurrentCulture Property to get or set the CultureInfo object that represents the culture used by the current thread. See: CultureInfo.CurrentCulture Property 

    From your description above, this discussion will help you on this. How can I change the CurrentCulture of the entire process (not just current thread) in .Net? 

    Please let me know if there is anything that I can do to help.

    Best regards.

    Angie

    Wednesday, January 14, 2015 1:04 AM
  • User1883319856 posted

    Hi Angie

    Thanks for answering my question. But the discussion you referenced in your answer deals about BackgroundWorker and how to change the culture in it. Thus it does not answer my question and I have marked your answer as "not answered".

    I'm still open for other ideas.

    Regards
    Aedu

    Friday, February 6, 2015 5:36 AM
  • User753101303 posted

    Hi,

    Have you checked the value for CurrentCulture? IMO the point is that doing that in Application_Start (that happens once when the application starts) don't have any effect for later requests.

    If you want to always use de-CH just change the web.config file : https://msdn.microsoft.com/en-us/library/vstudio/hy4kkhe0(v=vs.100).aspx (and culture, possibly uiCulture)

    If you want to decide based on some condition the article still applies IMO. Keep in mind that a web server is not a process that just starts. It uses quite numerous threads etc... that can be reused over time for distinct requests etc... so IMO you can't do just that in Application_Start and expect to be done with it (unless perhaps using this new 4.5 stuff). I added the article you were referred list to my "exploration" list (but once again if you want a fixed culture rather than to select the culture based on some condition, just use the web.config file).

    Not given a close look but http://blogs.msdn.com/b/tmarq/archive/2007/07/21/asp-net-thread-usage-on-iis-7-0-and-6-0.aspx should give an idea about how a web server works.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 6, 2015 5:58 AM