locked
Converting float values from different cultures RRS feed

  • Question

  • User758457977 posted
    Hello all.  I have an ASP.net application that is in five cultures.  I have a field called Amount.  This value is stored in a SQL Server database with column type of float.  If the current culture is set to french (fr-FR), the value is displayed as ',35'.  If the current culture is set to english (en-US), the value is displayed as '.35'. 

    When the user clicks the update button, I need to convert the Amount TextBox value from whatever culture the current page is in to en-US so I can store it in the database.  I would like to avoid writing code that involves any sort of case-statement that flows through all of the available cultures my website is available in (i.e. case "fr-FR": //replace commas with decimals).  Is there any kind of automatic conversion built into the framework that allows you to pass a value, the given culture, and the desired culture to convert to?  If not, what do you recommend as the best way to accomplish this?  I have hundreds of text boxes that I'll need to do this for as well. 


    Monday, August 7, 2006 2:03 PM

All replies

  • User758457977 posted
    I solved this problem on my own and thought I would post my solution.  This converts to en-US, but you could easily modify the code to supply the culture you want to convert to, create a CultureInfo object and then change the if/else statements to change to the NumberDecimalSeparator or NumberGroupSeparator for the cultureinfo you want:

        //Converts decimal values to en-US to store in database
        public static string formatFloat(string culture, string value)
        {
            CultureInfo cInfo = new CultureInfo(culture);
            char[] decSeparator = cInfo.NumberFormat.NumberDecimalSeparator.ToCharArray();
            char[] groupSeparator = cInfo.NumberFormat.NumberGroupSeparator.ToCharArray();

            if(value == null || value.Equals(""))
            {
                return "";
            }
            else
            {
                char[] _value = value.ToCharArray();
                for(int i = 0; i < _value.Length; i++)
                {
                    if(_value[i] == decSeparator[0])
                        _value[i] = '.';
                    else if(_value[i] == groupSeparator[0])
                        _value[i] = ',';
                }
                StringBuilder sb = new StringBuilder();
                for(int i = 0; i < _value.Length; i++)
                {
                    sb.Append(_value[i]);
                }
                return sb.ToString();
            }
        }

    Monday, August 7, 2006 4:18 PM
  • User758457977 posted
    Removed unneccessary for-loop from above code:

        //Converts decimal values to en-US to store in database
        public static string formatFloat(string culture, string value)
        {
            CultureInfo cInfo = new CultureInfo(culture);
            char[] decSeparator = cInfo.NumberFormat.NumberDecimalSeparator.ToCharArray();
            char[] groupSeparator = cInfo.NumberFormat.NumberGroupSeparator.ToCharArray();
           
            if(value == null || value.Equals(""))
            {
                return "";
            }
            else
            {
                StringBuilder sb = new StringBuilder();          
                char[] _value = value.ToCharArray();
                for(int i = 0; i < _value.Length; i++)
                {
                    if(_value[i] == decSeparator[0])
                        _value[i] = '.';
                    else if(_value[i] == groupSeparator[0])
                        _value[i] = ',';
                    sb.Append(_value[i]);
                }
                return sb.ToString();
            }
        }

    Monday, August 7, 2006 4:28 PM