简体   繁体   English

将文本框中的数字转换为浮点C#

[英]Convert number in textbox to float C#

I have textbox that accept numbers. 我有接受数字的文本框。 Those numbers will be saved in database. 这些号码将保存在数据库中。 When I enter number like 2,35 and convert to float and send to database I get error because database accept only number with dot, eg 2.35 当我输入2,35之类的数字并转换为float并发送给数据库时,由于数据库只接受带点的数字,例如2.35,因此出现错误

float num = float.Parse(textBox1.Text);

num is still 2,25 num仍然是2,25

How to manage that? 如何处理? I've tried with CultureInfo.InvariantCulture but I never get what I want 我已经尝试过CultureInfo.InvariantCulture但我从未得到想要的东西

You can try the following: 您可以尝试以下方法:

float.Parse(textBox1.Text.Trim(), CultureInfo.InvariantCulture.NumberFormat);

I hope this would've solved the issue. 我希望这可以解决问题。

The easiest way is to replace ',' with '.' 最简单的方法是将“,”替换为“。”。 in like: 在像:

float num = float.Parse(textBox1.Text);
string stringValue = num.ToString().Replace(',', '.');

Then send "stringValue" to database. 然后将“ stringValue”发送到数据库。

I hope that helps you. 希望对您有所帮助。

use this: 用这个:

CultureInfo.CreateSpecificCulture("en-US");

I have same problem back then and it solved by code above 那时我有同样的问题,上面的代码解决了

num is 2,25 because it's shown to you in your culture. num2,25因为它是根据您文化显示给您 It will be passed correctly to the database, provided you use the usual mechanisms (ie prepared statements with parameters). 如果您使用通常的机制(即带有参数的预备语句),它将被正确地传递到数据库。 If you insist on manually gluing together SQL, then by all means use InvariantCulture to format the number, but generally, please don't. 如果您坚持手动将SQL粘合在一起,则一定要使用InvariantCulture格式化数字,但通常不要这样做。

This is a common globalization issue. 这是一个普遍的全球化问题。 What you have to define is a single culture in which to store the data itself, since you are storing it as a string value. 您需要定义的是一种单一的区域性,用于存储数据本身,因为您将其存储为字符串值。 Then, do ALL your data input and handling using that culture. 然后,使用该区域性进行所有数据输入和处理。 In our code, we have several blocks that look similar to this in order to handle multi-cultural math and data display: 在我们的代码中,我们有几个看起来与此类似的块,以便处理多元文化的数学和数据显示:

//save current culture and set to english
CultureInfo current = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

//Do Math and Data things

//restore original culture
System.Threading.Thread.CurrentThread.CurrentCulture = current;

This way you can make sure that all the data is handled and stored the same way, regardless of the culture being use to generate or display the data. 这样,无论使用哪种区域性生成或显示数据,您都可以确保所有数据的处理和存储方式都相同。

Edit 编辑

To do the data save, and converting the number to a string, you would do things exactly the same way. 要保存数据并将数字转换为字符串,您将执行完全相同的操作。 While you have the current threads CultureInfo setting as "en-US", the .ToString() methods from all the numbers will use "." 当前线程的CultureInfo设置为“ en-US”时,所有数字中的.ToString()方法将使用“”。 instead of "," for the decimal point. 而不是“,”代表小数点。 The other way to do it is specify a format provider when calling .ToString(). 另一种方法是在调用.ToString()时指定格式提供程序。

decimalNumber.ToString(new CultureInfo("en-US"));

This specifies that when you convert the number to a string, use the NumberFormat from the provided culture. 这指定当您将数字转换为字符串时,请使用提供的区域性中的NumberFormat。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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