Converting number in string with negative sign at the end

From the MSDN documentation I should use NumberFormatInfo's NumberNegativePattern property to set the expected pattern for negative number values.

So I tried:

var format = new NumberFormatInfo {NumberNegativePattern = 3};
Console.WriteLine(Convert.ToDouble("1.000-", format));

But I always receive a FormatException saying "Input string was not in a correct format.". I also tried formatting with NumberFormatInfo.InvariantInfo - with same results.

Jon Skeet
people
quotationmark

You don't need a format here - it looks like the NumberNegativePattern is only used when formatting, not parsing, and then only for the N format. However, there's a NumberStyles value for this:

Console.WriteLine(double.Parse("1.000-", 
    NumberStyles.AllowTrailingSign | NumberStyles.AllowDecimalPoint));

people

See more on this question at Stackoverflow