开发者

Store number from in datatable but Culture specific

I have spent so much time trying to figure out how numbers work in different cultures in C#. I have sorted all the scenarios except when adding a number to datatable. In db I have number in US format (e.g. 1.32), in frontend if the culture is german then it show 1,32 in grid.

I can convert 1.32 to 1,32 using

Convert.ToDouble("1.32").ToString(Culture.GetCultureInfo("de-DE"))

but whe开发者_JS百科n I add this into a data table in a column of type double it becomes 132.0.

the number in db is of type double. This is how I try to convert it into proper culture format:

dt.Rows.Add(new Object[] { Convert.ToDecimal(ds.Tables[0].Rows[i][5]).ToString(culture.NumberFormat) })

One way would be to format the number in rowdatabound event of grid but is this a proper solution? There should be a way to store culture specific number in datatable.


When you save the value to the database/datatable it should be of type double not string. A double value does not care about formatting, only when it is converted (and displayed) as a string.

As for formatting goes, the GridView uses the culture specified in CultureInfo.CurrentCulture (can be changed from code). But you can also set you specific formatting on BoundField using property DataFormatString


If you attempt to add a string to a DataTable field of type double, it will be parsed using the CultureInfo.CurrentCulture - which presumably has period as a decimal separator in your case.

You should add the double directly to the DataTable, rather than formatting it as a string in a different culture (de-DE):

dataTable.Rows.Add(..., Convert.ToDouble("1.32"), ...);

rather than:

dataTable.Rows.Add(..., Convert.ToDouble("1.32").ToString(anotherCulture), ...);

Subsequently you can change the culture used to display the value from the DataTable - how you do this depends on how you are displaying it, but one way is:

((double) dataRow["MyDoubleColumn"]).ToString(anotherCulture);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜