-
Notifications
You must be signed in to change notification settings - Fork 96
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
Int32.TryParse and F#+ tryParse give different results #541
Comments
Related: regardless of whether we'd fix this issue or not, perhaps we can update the documentation to specify that |
Yes, I think it makes sense to have it documented as using invariant culture. |
As always with parsers there's the question of how smart my parser should be, where do you draw the line? Our current parsers uses The problem is, parsers are kind of the inverse of printers, but it's not bijective as there are many ways to print and many ways to parse. We can only implement one way to print, however making parsers so strict that they only can parse that specific printing way could be a nice property but a pain to work with in real world scenarios. So we can come up with this property: How different styles we allow? That's an interesting discussion (thanks for kicking it off), I think we should think in practical cases. Then as you say we have the compatibility problem, if we eventually decide to make them more strict, but we can always implement a |
While I agree with everything you say, the problem with Since this is about calling the parse methods of .NET, I think, apart from forcing Invariant Culture, that we should stay as close as possible to what .NET itself implements, to stick with the "principle of least suprise". Certainly, creating invalid parsing functions will not have been the goal here. But changing this leads to a backwards compat issue. I'd vote for your proposal with parseStrict. Note that this is not the same as ParseExact, as the standard Parse functions on number types are quite strict already. The only big problem with these is the absence of invariant culture in the default behavior. |
OK, let's go with |
I would set the defaults to what I wrote before:
not sure if |
Description
Basically (tested with culture on
en-US
).In other words, integers cannot have thousand-separators with
Int32.TryParse
, but can withtryParse
.Conversely, it doesn't allow decimals, which is good:
Likewise there's a similar difference with
e-notation
:Repro steps
See above.
Expected behavior
From the description of the method, you'd expect the same behavior as the .NET functions, but they allow certain illegal values, or at least ambiguous, considering how .NET parses numbers.
Actual behavior
See above. This is caused by
NumberStyles.Any
, which is not used byInt32.TryParse
orDecimal.TryParse
. These useNumberStyles.Integer
andNumberStyles.Decimal
respectively (see source of .NET), which limit the allowed range.Known workarounds
Create your own types or call
Int32.TryParse
directly, or create your own helper function.Related information
Most recent F#+ and tested with .NET 6, but assuming it is the same on .NET 7.
The text was updated successfully, but these errors were encountered: