Search code examples
c#parsingdoubleinfinity

How should I parse Infinity with Double?


If I try

Double.Parse("Infinity")

I get

Double.Parse("Infinity") threw an exception of type 'System.FormatException'

Why? And what should I do if I want to parse it anyway and get a Double with the value Infinity?


Solution

  • I just found this out:

    Decimal.Parse("Infinity", System.Globalization.CultureInfo.InvariantCulture);
    

    will work and return a double with the value +Infinity.

    The reason it did not work is that I am not, I think, automatically in the InvariantCulture but perhaps in the de-DE culture which does not handle the exact string "Infinity". (Perhaps it would handle some other string.)