Skip to content

Commit

Permalink
Added "NumberStyles.AllowDecimalPoint" to the number styles when pars…
Browse files Browse the repository at this point in the history
…ing ints and longs

This allows values like "3.00" to be correctly converted, but still not values like "3.14".
  • Loading branch information
abjerner committed Aug 27, 2022
1 parent 5b17b47 commit de37785
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Skybrud.Essentials/Strings/StringUtils.Int32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static int ParseInt32(string input, int fallback) {
/// <see cref="int.MinValue"/> or greater than <see cref="int.MaxValue"/>.</param>
/// <returns><c>true</c> if <paramref name="input"/> was converted successfully; otherwise, <c>false</c>.</returns>
public static bool TryParseInt32(string input, out int result) {
return int.TryParse(input, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
return int.TryParse(input, NumberStyles.Integer | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out result);
}

/// <summary>
Expand All @@ -65,7 +65,7 @@ public static bool TryParseInt32(string input, out int result) {
/// <see cref="int.MinValue"/> or greater than <see cref="int.MaxValue"/>.</param>
/// <returns><c>true</c> if <paramref name="input"/> was converted successfully; otherwise, <c>false</c>.</returns>
public static bool TryParseInt32(string input, out int? result) {
if (int.TryParse(input, NumberStyles.Integer, CultureInfo.InvariantCulture, out int temp)) {
if (int.TryParse(input, NumberStyles.Integer | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out int temp)) {
result = temp;
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Skybrud.Essentials/Strings/StringUtils.Int64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static long ParseInt64(string input, long fallback) {
/// <see cref="long.MinValue"/> or greater than <see cref="long.MaxValue"/>.</param>
/// <returns><c>true</c> if <paramref name="input"/> was converted successfully; otherwise, <c>false</c>.</returns>
public static bool TryParseInt64(string input, out long result) {
return long.TryParse(input, NumberStyles.Integer, CultureInfo.InvariantCulture, out result);
return long.TryParse(input, NumberStyles.Integer | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out result);
}

/// <summary>
Expand All @@ -65,7 +65,7 @@ public static bool TryParseInt64(string input, out long result) {
/// <see cref="long.MinValue"/> or greater than <see cref="long.MaxValue"/>.</param>
/// <returns><c>true</c> if <paramref name="input"/> was converted successfully; otherwise, <c>false</c>.</returns>
public static bool TryParseInt64(string input, out long? result) {
if (long.TryParse(input, NumberStyles.Integer, CultureInfo.InvariantCulture, out long v)) {
if (long.TryParse(input, NumberStyles.Integer | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out long v)) {
result = v;
return true;
}
Expand Down

0 comments on commit de37785

Please sign in to comment.