Search code examples
c#globalization

When changing C# web application to spanish, calculation is not working


I am working on a web application and I have to change my application to support Spanish language(It is in english). Now calculation is not coming correctly.

For showing the problem in calculation I made another web application and set its current culture to "es-ES". I have one textbox and its text I have hard coded to 40.00 like following:

<asp:TextBox ID="txtNumber" Text="40.00" runat="server"  ></asp:TextBox>

I am facing problems when I am trying to convert it:

Convert.ToDecimal(txtNumber.Text);    //4000
decimal.Parse(txtNumber.Text, CultureInfo.InvariantCulture);   //40,00

Which is not what I expected. decimal.parse is correct but I want (.) instead of (,). Its a pain to change every control like that for a large application also because of the fact that I had used

<ajaxtoolkit:filteredtextboxextender id="FilteredTextBoxExtender1" runat="server" targetcontrolid="txtNumber" filtertype="Custom, Numbers" validchars="." />

many places. As you can see it will allow only (.) and not (,) so user won't be able to input decimal numbers. Is it possible to have all calculation in english culture and show application in spanish?


Solution

  • You can try changing the decimal separator for the current thread.

    Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator = ".";
    Thread.CurrentThread.CurrentUICulture.NumberFormat.NumberDecimalSeparator = ".";