将文本框中的数字转换为浮动 C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15811215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert number in textbox to float C#
提问by Josef
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, e.g. 2.35
我有接受数字的文本框。这些数字将保存在数据库中。当我输入像 2,35 这样的数字并转换为浮点数并发送到数据库时,我收到错误,因为数据库只接受带点的数字,例如 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
但我从来没有得到我想要的
回答by Igor Frank
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.
我希望这对你有帮助。
回答by Harits Fadillah
use this:
用这个:
CultureInfo.CreateSpecificCulture("en-US");
I have same problem back then and it solved by code above
我当时有同样的问题,它通过上面的代码解决了
回答by Joey
num
is 2,25
because it's shown to you in yourculture. It will be passed correctly to the database, provided you use the usual mechanisms (i.e. 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.
num
是2,25
因为它在你的文化中展现给你。如果您使用通常的机制(即带参数的准备好的语句),它将被正确传递到数据库。如果您坚持手动将 SQL 粘合在一起,那么InvariantCulture
请务必使用格式化数字,但一般情况下,请不要使用。
回答by Nevyn
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 "." instead of "," for the decimal point. The other way to do it is specify a format provider when calling .ToString().
要进行数据保存,并将数字转换为字符串,您可以采用完全相同的方式进行操作。当您将当前线程 CultureInfo 设置为“en-US”时,.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。
回答by ShaileshDev
You can try the following:
您可以尝试以下操作:
float.Parse(textBox1.Text.Trim(), CultureInfo.InvariantCulture.NumberFormat);
I hope this would've solved the issue.
我希望这已经解决了这个问题。