CultureInfo problem ( number decimal separator )


I'm from Croatia and my culture info is hr-HR which means that my number decimal separator is ',' (comma). The problem happens when you are creating string for a path data. When you append double value to a StringBuilder, default system culture info is used and double values in path data string looks like this: "M0,0 L57,8673502604167,0 A57,8673502604167 57,8673502604167 1 0 0 -22,2072390486352,-53,4365863430526 z" ( you can notice that there are no points in string, only commas ).
I temporary fixed this problem for myself this way:
I added one more class to FreeSilverlightChart project.
public class Shared
    public static CultureInfo _decimalSeparatorPointCultureInfo = null;
    static Shared()
        _decimalSeparatorPointCultureInfo = new CultureInfo("en-US");  // US because decima sep. is point
    public static CultureInfo DecimalSeparatorPoint
        get { return _decimalSeparatorPointCultureInfo; }
and everywhere where you append double to a StringBuilder i did this:
    StringBuilder sb = new StringBuilder();
    sb.Append("M0,0 L");
and final result was: "M0,0 L57.8673502604167,0 A57.8673502604167 57.8673502604167 1 0 0 -22.2072390486352,-53.4365863430526 z"
I hope fix for this will be included in next releases.....


RuneBivrin wrote Mar 10, 2010 at 10:46 PM

I just went through this and fixed all (I think) uses of Append. The simple solution is to add

System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;

to the Page_Loaded() method.

The better way is to change all Append to AppendFormat.

Thus your code becomes

StringBuilder sb = new StringBuilder();
sb.AppendFormat(CultureInfo.InvariantCulture, "M0,0 L{0},{1}", x1, y1);

Much more compact and easier to read. Quite possibly a smidgen faster as well.

wrote Mar 10, 2010 at 10:47 PM

wrote Feb 14, 2013 at 2:25 AM