简体   繁体   English

将字符串转换为浮点数时,为什么会出现FormatException?

[英]Why do I get a FormatException when converting a string to a float?

When I try to convert a string to float: 当我尝试将字符串转换为float时:

Console.WriteLine(float.Parse("6.59"));

it throws an exception: 它抛出一个异常:

Unhandled Exception: System.FormatException: Input string was not in a correct f ormat. 未处理的异常:System.FormatException:输入字符串的格式不正确。
at System.Number.ParseSingle(String value, NumberStyles options, NumberFormat Info numfmt) 在System.Number.ParseSingle(字符串值,NumberStyles选项,NumberFormat信息numfmt)

When I try it like this: 当我这样尝试时:

Console.WriteLine(Convert.ToSingle("6.59"));

It throws the same exception: 它抛出相同的异常:

Unhandled Exception: System.FormatException: Input string was not in a correct f ormat. 未处理的异常:System.FormatException:输入字符串的格式不正确。
at System.Number.ParseSingle(String value, NumberStyles options, NumberFormat Info numfmt) 在System.Number.ParseSingle(字符串值,NumberStyles选项,NumberFormat信息numfmt)
at System.Convert.ToSingle(String value) 在System.Convert.ToSingle(字符串值)

Can you explain why this happens? 您能解释为什么会这样吗?

The single argument Parse method uses the current culture to parse the string. 单参数Parse方法使用当前区域性来分析字符串。 If your current culture uses some other decimal separator, this will fail. 如果您当前的区域性使用其他十进制分隔符,则将失败。

Try using the invariant culture: 尝试使用不变文化:

float.Parse("6.59", CultureInfo.InvariantCulture)

The problem here is your culture. 这里的问题是你的文化。

Either set the invariant culture like this: 设置像这样的不变文化:

float.Parse("6.59", CultureInfo.InvariantCulture)

or use the correct decimal separator for your culture 或为您的文化使用正确的十进制分隔符

float.Parse("6,59")

I wonder why you are using a literal string. 我想知道为什么您要使用文字字符串。 If you are having problems entering literal floats, you can use 如果您在输入文字浮点数时遇到问题,可以使用

Console.WriteLine(6.59f)

If you do it this way culture doesn't matter because the value is decided at compile time. 如果采用这种方式,文化就没有关系,因为价值是在编译时决定的。

You are probably using a culture that uses the , as a decimal seperator. 您可能正在使用的是使用了一种文化,作为小数分隔符。

You could try to Parse using the InvariantCulture : 您可以尝试使用InvariantCulture进行解析

float.Parse("6.59", CultureInfo.InvariantCulture)

There could be problem with Locale/Culture. 语言环境/文化可能存在问题。 You need to set , instead of . 您需要设置,而不是. for the decimal separator. 小数点分隔符。

Culture - specific things. 文化-特定的事物。 What's your default culture? 您的默认文化是什么? Some cultures use "," instead of ".". 有些文化使用“,”代替“。”。 You can try this: 您可以尝试以下方法:

float.Parse("6.59", CultureInfo.InvariantCulture);

I know everyone here has already given the reason for the problem experienced but perhaps somebody should just expand on why Invariant fixes it. 我知道这里的每个人都已经给出了遇到此问题的原因,但是也许有人应该就Invariant修复它的原因进行扩展。

The CultureInfo class is used either directly or indirectly by classes that format, parse, or manipulate culture-specific data, such as String, DateTime, DateTimeOffset, and the numeric types to deal with the differences in the way different cultures write these types. CultureInfo类由格式化,解析或处理区域性特定数据的类(例如String,DateTime,DateTimeOffset和数字类型)直接或间接使用,以处理不同区域性写入这些类型的方式上的差异。

In case of the decimal type some cultures use a period(.) whilst others use a comma (,). 对于十进制类型,某些区域性使用句点(。),而另一些区域使用逗号(,)。 By default when you are using the Conversion Libraries it will make use of your local culture (that is the country your OS to configured for). 默认情况下,当您使用转换库时,它将利用您的本地文化(即您要配置操作系统的国家/地区)。

By specifying Invariant you say that you expect thousand separators to be commas(,) and decimal deliminator to be a period(.) as it is in most cultures. 通过指定不变式,您可以说与大多数区域性一样,您希望千位分隔符为逗号(,),十进制分隔符为句点(。)。

A big problem that sometimes happens is that these cultural conventions change from the OS point of view. 有时会发生的一个大问题是,从OS的角度来看,这些文化习俗会发生变化。 For example the South African (ZA) culture info used to behave like the invariant culture. 例如,南非(ZA)的文化信息过去表现得像不变文化。 Microsoft changed this with Windows 8 where the decimal suddenly became a comma and the thousand separator a space.This resulted in many legacy systems written in .Net suddently breaking when one migrated them to newer operating systems. 微软在Windows 8中对此进行了更改,在Windows 8中,小数点突然变成了逗号,千位分隔符变成了空格。这导致许多用.Net编写的旧系统在将其迁移到较新的操作系统时突然中断。

In the end, deal normalize all local culture info to invariant and persist and deal with them in your business logic in this format. 最后,deal将所有本地文化信息标准化为不变的,并以这种格式在您的业务逻辑中进行处理。 Then localize it back on the front end. 然后将其本地化回前端。 Same goes for DateTime, as soon as possible convert to UTC, and only back when you render an output. 对于DateTime也是如此,尽快转换为UTC,只有在呈现输出时才返回。

You could also try Convert class to perform this task. 您也可以尝试Convert类来执行此任务。

Convert.ToDecimal("6.59"); Convert.ToDecimal(“ 6.59”);

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

相关问题 为什么我会使用 NLog 获得 FormatException? - Why do I get FormatException with NLog? C#当我使用TryParse时,为什么会收到“未处理的异常:System.FormatException:输入字符串的格式不正确。” - C# Why I get “Unhandled Exception: System.FormatException: Input string was not in a correct format.” when I use TryParse? FormatException,将字符串转换为long - FormatException, converting a string to a long 从字符串解析为float时如何修复unity FormatException? - How to fix unity FormatException when parse from string to float? 为什么将字符串路径转换为Geometry然后再次返回会引发FormatException? - Why converting a string path to Geometry and back again throws a FormatException? 将Base64字符串转换为字节时出现FormatException - FormatException when converting Base64 string to bytes 在C#中将字符串转换为float时,为什么逗号(“,”)消失了。 - Why does comma (“,”) disappear when converting string to float in c#. 将字符串转换为DateTime时发生FormatException - FormatException while converting string to DateTime 将 String 转换为 Int 会导致 FormatException - Converting String to Int causes FormatException 转换为八进制时出现奇怪的FormatException - Strange FormatException when converting to octal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM