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

18,907

Solution 1

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)

Solution 2

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.

Solution 3

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)

Solution 4

Culture - specific things. What's your default culture? Some cultures use "," instead of ".". You can try this:

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

Solution 5

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.

Share:
18,907

Related videos on Youtube

Edward83
Author by

Edward83

Updated on May 28, 2022

Comments

  • Edward83
    Edward83 almost 2 years

    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?

  • R. Martinho Fernandes
    R. Martinho Fernandes about 13 years
    Portuguese is even worse. Here . is the digit group separator (aka thousands separator). Because of this float.Parse("6.59") does not fail, but the result is 659. This has wasted me a couple of hours in the past :(
  • R. Martinho Fernandes
    R. Martinho Fernandes about 13 years
    The UK is Europe too! (I assumed the literal string was the result of trying to find "the simplest code that reproduces the problem").
  • Jonas Van der Aa
    Jonas Van der Aa about 13 years
    Blasted imperial units! Go Metric! (just kidding) I forgot that the UK still uses the imperial system :) But my point still stands, if he's going to put string literals into float.Parse he's doing it all wrong.
  • Andre Lombaard
    Andre Lombaard about 11 years
    +1 for this useful tip, my code was working on my development environment but not in the production environment.