我怎样才能把“。”在文本框中输入小数值时,小数点指向“,”逗号?

How can I turn "." decimal points to "," comma when entering a decimal value in a textbox?

请帮忙解决这个问题。我没有找到解决方案。

我构建了一个允许输入一些数据的 WebForm(来自多个控件,一个 TextBox 包含一个表示数量的数字)。 WebForm 在 ASP.NET 4.0(使用 C#)中。

WebForm 将控件中的所有值传递到托管在 SQL Server 2014 版本上的数据库中。问题来了:Web 应用程序应该 运行 在德语环境中。 (我的机器和其他可以访问该应用程序的机器)。

我的问题是,即使将区域设置设置为德语,我仍然必须输入带“.”的小数值。小数点,而不是在数量文本框中使用“,”逗号。

If I enter the decimal values with commas, they get glued in a single integer, rather than going to decimals. e.g. 11123,34 is recorded in the table as 1112324 1123.45 is recorded in the table as 1123,45

注意:我在数据库中设置字段,这里TextBox地址为"Money"类型,在SQLtable定义中。

When accessing the contents of the table (as written) via a GridView control in another webform, I get all the values with "," commas , irrespective if the Regional Settings are set to German or English.

为 Quantity 存储的数值格式将其显示方式从“,”逗号更改为“.”仅当 运行 直接在 SQL 服务器上 table 上选择 SELECT 时(我的意思是在 SQL Server Management Studio 中),或者当我 "SET LANGUAGE TO GERMAN/BRITISH" 在 SQL 服务器上。

我的问题是:

I do not want users to use the decimal separator of "." when entering the quantity, and use the decimal separator "," when reading the quantity out of the database.

非常感谢您的帮助。

正确的解决方案:每个 将值转换为文本并返回的操作必须适当地指定区域性。

   // formatting/parsing 
   var userCuluture = new CultureInfo("de-de");
   textBox.Text = String.Format(userCuluture, "{0} for {1}", DateTime.Now, 1.99);
   var value = float.Parse(textBoxAmount.Text, userCulture);

   dbProvider.WriteValue(value); // use InvariantCulture inside for formating

可怜的人版本:更改线程的文化,以便使用默认文化进行转换并调整它,以便在用户的文化设置为当前时完成对用户数据的操作,并在 InvariantCulture 时完成数据库操作:

   // Manipulation of user's data to use "userCulutre" rules
   Thread.CurrentThread.CurrentCulture = userCulture;
   textBox.Text = String.Format("{0} for {1}", DateTime.Now, 1.99);
   var value = float.Parse(textBoxAmount.Text);

   // DB/file manipulations to use Invariant rules
   Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
   dbProvider.WriteValue(value); // use current culture for formatting/parsing

有很多方法可以管理当前的文化,您可以通过 https://www.bing.com/search?q=asp.net+culture, like Set Culture in an ASP.Net MVC app

找到这些方法