C# 转换带有句点而不是逗号的浮点数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/359298/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:26:40  来源:igfitidea点击:

Convert Float that has period instead of comma?

c#.net.net-2.0

提问by Mister Dev

I have data from a table in a database (string) that contain text and price. I extract the price from the data but my problem is that sometime I can Convert it to float and sometime not.

我从包含文本和价格的数据库(字符串)中的表中获取数据。我从数据中提取价格,但我的问题是有时我可以将其转换为浮动,有时则不能。

I have noticed that :

我注意到 :

Convert.ToSingle(m.Groups[1].Value);

It works but not always because sometime the period is the problem (it requires a comma). What can I do? I have try to replace the ".", by "," but sometime on other PC it's a period that it's required!

它有效但并不总是因为有时句号是问题(它需要逗号)。我能做什么?我曾尝试用“,”替换“.”,但有时在其他PC上需要一段时间!

采纳答案by Patrick Desjardins

You have this problem because the conversion check the language of your PC. You will need to do something like :

您遇到此问题是因为转换会检查您 PC 的语言。您将需要执行以下操作:

Convert.ToSingle(m.Groups[1].Value, CultureInfo.InvariantCulture.NumberFormat);

This ways, it won't check the language of the PC. You can find more information about InvariantCulturefrom MSDN. I have something similar in a project and my conversion works.

这样,它就不会检查 PC 的语言。您可以从 MSDN 中找到有关InvariantCulture 的更多信息。我在一个项目中有类似的东西,我的转换工作。

回答by Christoffer Lette

As others have said:

正如其他人所说:

Convert.ToSingle(m.Groups[1].Value, CultureInfo.InvariantCulture);

You must also make sure that you use the InvariantCulturewhen writingto the database. (It would be even better if you saved the data to a column with its native data type, but I digress...)

您还必须确保InvariantCulture写入数据库时使用。(如果您将数据保存到具有其本机数据类型的列中会更好,但我离题了......)

回答by ICR

If you don't have write access to the database the first thing to do is try and convince the sources of the data to use the invariant culture. If the data is being inputted by the user you can do something like:

如果您没有对数据库的写访问权限,首先要做的是尝试说服数据源使用不变区域性。如果用户正在输入数据,您可以执行以下操作:

float f = float.Parse(input);
string toDb = f.ToString(CultureInfo.InvariantCulture);

And then from the other side:

然后从另一边:

float f = float.Parse(fromDb, CultureInfo.InvariantCulture);
string toOutput = f.ToString();

Though if you can convince them of that it's probably better, as Lette says, to convince them to use the native data type.

但是,如果您可以说服他们相信,那么正如 Lette 所说,说服他们使用本机数据类型可能会更好。

I would also, as can be seen from the snippets above, recomment using float.Parse over Convert for a variety of reasons, but the most significant being the ability to use TryParse:

从上面的片段中可以看出,出于各种原因,我还建议使用 float.Parse 而不是 Convert,但最重要的是能够使用 TryParse:

float f;
if (!float.TryParse(input, out f))
{
    // ERROR
}

回答by baretta

Or ask for this specific number format explicitly:

或明确要求此特定数字格式:

System.Globalization.NumberFormatInfo nf
  = new System.Globalization.NumberFormatInfo ( )
{
  NumberGroupSeparator = "."
};
float f = float.Parse ( "5.34534", nf );