I am trying to parse strings that may have a decimal. I'm using Int32.TryParse but it doesn't seem to be working.
Here is my function where I try to parse:
private NumberStyles styles = NumberStyles.Number;
private CultureInfo culture = CultureInfo.CurrentCulture;
public int? ParseInt(string node)
{
int number;
if (Int32.TryParse(node, styles, culture.NumberFormat, out number))
{
return number;
}
return null;
}
And here is my unit test that is failing
[Fact]
public void TestParseIntWithDecimal()
{
string provided = "28.789";
int expected = 28;
int? actual = _parser.ParseInt(provided);
Assert.NotNull(actual);
Assert.Equal(expected, actual);
}
When I run my unit test, null is returned from ParseInt and I don't understand why. I thought TryParse could parse decimal numbers into integer numbers.
AllowThousandswithAllowDecimalPoint, where the former can letint.Parse()parse numbers with commas (depending on culture). In some cultures comma is the decimal separator. It does work if the fractional digits only contain zeroes (as documented): ideone.com/stQ9GY.