locked
Dots and Commas

    Question

  • Calling Float.ToString() will generate a string where the decimal part is separated by a comma (on my Swedish comp). I'd like it to be a dot instead, mainly for compatibility reasons (file outputs etc...). Is there some easy way to change this, like some locale setting somewhere? I'd prefer not to call String.Replace() every time I call ToString().

    Using C#.NET on MSVC2005.NET

    Monday, January 15, 2007 2:20 PM

Answers

  • One of the ToString overloads take a IFormatProvider parameter so you can do something like:

    System.Globalization.NumberFormatInfo format = new System.Globalization.NumberFormatInfo();

    format.NumberDecimalSeparator = ".";

    format.NumberGroupSeparator = ",";

    s = f.ToString(format);

    In your specific case (using . as decimal separator) you can make it even more simple:

    s = f.ToString(System.Globalization.NumberFormatInfo.InvariantInfo); // NumberFormatInfo.InvariantInfo uses . as a decimal separator

     

    Monday, January 15, 2007 2:33 PM

All replies

  • One of the ToString overloads take a IFormatProvider parameter so you can do something like:

    System.Globalization.NumberFormatInfo format = new System.Globalization.NumberFormatInfo();

    format.NumberDecimalSeparator = ".";

    format.NumberGroupSeparator = ",";

    s = f.ToString(format);

    In your specific case (using . as decimal separator) you can make it even more simple:

    s = f.ToString(System.Globalization.NumberFormatInfo.InvariantInfo); // NumberFormatInfo.InvariantInfo uses . as a decimal separator

     

    Monday, January 15, 2007 2:33 PM
  • Seemed to work fine. Thanks for good answer.
    Monday, January 15, 2007 2:44 PM
  • you can also try ToString("#.000")

     

     

    Monday, January 15, 2007 3:01 PM