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

后端 未结 7 1503
傲寒
傲寒 2021-02-19 06:46

When I try to convert a string to float:

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

it throws an exception:

Unhandled Exc

相关标签:
7条回答
  • Culture - specific things. What's your default culture? Some cultures use "," instead of ".". You can try this:

    float.Parse("6.59", CultureInfo.InvariantCulture);
    
    0 讨论(0)
  • 2021-02-19 07:06

    There could be problem with Locale/Culture. You need to set , instead of . for the decimal separator.

    0 讨论(0)
  • 2021-02-19 07:06

    You could also try Convert class to perform this task.

    Convert.ToDecimal("6.59");

    0 讨论(0)
  • 2021-02-19 07:10

    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.

    0 讨论(0)
  • 2021-02-19 07:12

    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)
    
    0 讨论(0)
  • 2021-02-19 07:13

    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.

    0 讨论(0)
提交回复
热议问题