Search code examples
c#.netnumber-formattingformatexception

Decimal.Parse and Double.Parse System.FormatException Different behavior


I do not understand why I have to specify NumberStyles.Float when using decimal.Parse and not for double.Parse

I can do:

var tmp = double.Parse("1e-2");

but not:

var tmp1 = decimal.Parse("1e-2"); 

Because a System.FormatException (Input string was not in a correct format) is thrown

var tmp1 = decimal.Parse("1e-2", System.Globalization.NumberStyles.Float);

someone can tell me if there is a good reason behind this behavior


Solution

  • It's just behaving as documented. From Double.Parse:

    The s parameter is interpreted using a combination of the NumberStyles.Float and NumberStyles.AllowThousands flags.

    Note that NumberStyles.Float includes NumberStyles.AllowExponent.

    From Decimal.Parse:

    Parameter s is interpreted using the NumberStyles.Number style.

    NumberStyles.Number does not include NumberStyles.AllowExponent.

    I can't reproduce your bizarre stack traces which appear to show the same call failing just after it's worked:

    • Decimal.Parse("1e-2") always fails for me
    • Decimal.Parse("1e-2", NumberStyles.Float) always works for me
    • Double.Parse("1e-2") always works for me
    • Double.Parse("1e-2", NumberStyles.Float) always works for me

    As for why the "default" number style differs between the two - I suspect it's because double values typically are used in scientific scenarios where exponent-based representations are common, but decimal values typically aren't.