简体   繁体   中英

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

When I try to convert a string to float:

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

it throws an exception:

Unhandled Exception: System.FormatException: Input string was not in a correct f ormat.
at System.Number.ParseSingle(String value, NumberStyles options, NumberFormat Info 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.
at System.Number.ParseSingle(String value, NumberStyles options, NumberFormat Info numfmt)
at System.Convert.ToSingle(String value)

Can you explain why this happens?

The single argument Parse method uses the current culture to parse the string. 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 :

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.

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.

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. For example the South African (ZA) culture info used to behave like the invariant culture. 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.

In the end, deal normalize all local culture info to invariant and persist and deal with them in your business logic in this format. 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.

You could also try Convert class to perform this task.

Convert.ToDecimal("6.59");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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