本文介绍了如何将字符串转换为加倍适当的CultureInfo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中的两个nvarchar的字段存储的数据类型和默认值,我有一个数据类型双和值65.89875英语格式。

I have two nvarchar fields in database to store the DataType and DefaultValue, I have a DataType Double and value as 65.89875 in english format.

现在我希望用户看到的值按选定的浏览器语言格式(英文65.89875德国应显示为65,89875)。现在,如果用户编辑从德国格式65,89875这是65.89875等同于英语,而其他用户的意见,从英语的浏览器,此番6589875。

Now I want the user to see the value as per the selected browser language format (65.89875 in English should be displayed as 65,89875 in german). Now if the user edits from german format to 65,89875 which is 65.89875 equivalent in english, and the other user views from english browser it comes as 6589875.

这是因为在DB它保存为65,89875在nvarchar列,并在使用时变​​得6589875因为它认为英国的文化转变,作为分隔符是十进制运算符德。

This happens because in DB it was stored as 65,89875 in nvarchar column and when converted using english culture it becomes 6589875 since it considers , as seperator which is decimal operator for german.

任何想法我如何得到这个工作的所有的浏览器?

Any Idea how I get this working for all the browsers?

推荐答案

您需要定义将用于存储在数据库中的数据的单一语言环境,不变的文化是有出于这样的目的。

You need to define a single locale that you will use for the data stored in the database, the invariant culture is there for exactly this purpose.

在显示转换为自身的类型,然后格式化用户的文化。

When you display convert to the native type and then format for the user's culture.

例如。以显示:

string fromDb = "123.56";
string display = double.Parse(fromDb, CultureInfo.InvariantCulture).ToString(userCulture);

存储:

string fromUser = "132,56";
double value;
// Probably want to use a more specific NumberStyles selection here.
if (!double.TryParse(fromUser, userCulture, NumberStyles.Any, out value)) {
  // Error...
}
string forDB = value.ToString(CultureInfo.InvariantCulture);

PS。它差不多,不用说,使用一个数据类型相匹配的数据的列就更好了(但有时遗产范围内)。

PS. It, almost, goes without saying that using a column with a datatype that matches the data would be even better (but sometimes legacy applies).

这篇关于如何将字符串转换为加倍适当的CultureInfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 03:51