I'm calling the following two lines. The second line crashes.:
var a = long.Parse("2,147,483,648", NumberStyles.AllowThousands);
var b = long.Parse("-2,147,483,648", NumberStyles.AllowThousands);
However, if I modify the values to not have ',' characters and remove the NumberStyles enum it works. e.g.
var a = long.Parse("2147483648");
var b = long.Parse("-2147483648");
Am I doing something wrong? Is this a known issue? Is there an acceptable work-around that doesn't involve hacky string manipulation?
edit I should have mentioned the exception is a System.FormatException
, "Input string was not in a correct format."
For your second example, you need to use AllowLeadingSign
as well since you using NegativeSign
in your string.
var b = long.Parse("-2,147,483,648",
NumberStyles.AllowThousands | NumberStyles.AllowLeadingSign);
When you use long.Parse(string)
overload, this method uses NumberStyles.Integer
composite style which has already includes AllowLeadingSign
itself.
From reference source;
Integer = AllowLeadingWhite | AllowTrailingWhite | AllowLeadingSign,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With