简体   繁体   中英

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?

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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM