asp.netglobalizationcultureinfoasp.net-webcontrolcurrentuiculture

ASP.NET webcontrols render wrong in different culture settings


I have a self defined web control.

Some code in a loop:

double cellHeight = 12.34;
Label dcell = new Label();
dcell.Style["height"] = cellHeight  + "pt";
dcell.Text = cellHeight;

If I use CultureInfo("cs-CZ")

System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("cs-CZ");
System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("cs-CZ");

after render, the html came out

<span style="height:11,75pt">11,75</span>

actually what I expected is:

<span style="height:11.75pt">11,75</span> 

height:11,75pt is totally wrong when rendered in browser, actually the browser does not consider 11,75pt as 11.75pt.

However I need to keep the text field displayed based on culture info: the text field displays 11,75 that is correct.

So this is the problem - how can I fix?


Solution

  • You need to convert double to string properly, for example:

    dcell.Style["height"] = cellHeight.ToString("F", CultureInfo.CreateSpecificCulture("eu-ES")) + "pt";
    

    Or like this:

    NumberFormatInfo nfi = new NumberFormatInfo();
    nfi.NumberDecimalSeparator = ".";
    dcell.Style["height"] = cellHeight.ToString(nfi) + "pt";