-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
.Net 5.0 Int.Parse fails parsing with negative values. #40258
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
@eriksimonic what is the current culture?
and OS? |
@EgorBo If I change the culture to CultureInfo.InvariantCulture, the parse works. |
Console.WriteLine(string.Join(", ", Encoding.Unicode.GetBytes("-"))); // en-US negative sign
Console.WriteLine(string.Join(", ", Encoding.Unicode.GetBytes("−"))); // sl-SI negative sign
sl-SI locale uses a slightly different negative sign 🙂 You can obtain it via |
@EgorBo , this works correctly in .net core 5.0 or any other, It is this a "feature" |
hm... indeed, 🤔 Console.WriteLine(int.Parse("-1", new CultureInfo("sl-SI"))); works for |
Hasn't net5 moved to ICU on windows also? |
Yes. net5 switched to ICU library on Windows. Some cultures (e.g. en-US) work with multiple minus signs |
@eriksimonic as a workaround you can define an env. variable |
Yes this is by design as we switched to ICU. |
You could also write a method that tries both int ParseInt(string value)
{
if (int.TryParse(value, out int result))
{
return result;
}
return int.Parse(value, CultureInfo.InvariantCulture);
} |
@benaadams , hard to do when third party library uses the method. |
@eriksimonic - Outside the move to ICU, this sort of thing is always possible; you can construct/use custom cultures on windows that would have the same effect. It might be handy to know what sort of third-party library you're using here; for instance, if you're doing data serialization/processing, you should likely be in the invariant culture anyways. Although I'd probably raise a bug with them if you can't override the culture/formatting options. |
@eriksimonic, the answer of @Clockwork-Muse is correct. .NET 5.0 is by default using ICU and NLS. you have the option to switch back to NLS. but I wouldn't recommend that because NLS data can change too especially Windows working hard to converge with CLDR data which is shipped with ICU. As @Clockwork-Muse mentioned, if you want to guarantee the parsing, you should format and parse using Invariant culture. |
.Net 5.0 Preview 7, fails on Int.Parse("-1") with
Message:
System.FormatException : Input string was not in a correct format.
Stack Trace:
Number.ThrowOverflowOrFormatException(ParsingStatus status, TypeCode type)
Number.ParseInt32(ReadOnlySpan`1 value, NumberStyles styles, NumberFormatInfo info)
Int32.Parse(String s)
NetCore50prev7_Tests.NegativeIntegerFailsParse() line 15
public class NetCore50prev7_Tests
{
[Fact]
public void NegativeIntegerFailsParse()
{
int.Parse("-1");
}
}
The text was updated successfully, but these errors were encountered: