diff --git a/VectoCommon/VectoCommon/Utils/StringExtensionMethods.cs b/VectoCommon/VectoCommon/Utils/StringExtensionMethods.cs index c89db25926b684630a9a98feba059d7ec3450a9b..6a91bd48eca9531cc55a55290826a18ddb4c3812 100644 --- a/VectoCommon/VectoCommon/Utils/StringExtensionMethods.cs +++ b/VectoCommon/VectoCommon/Utils/StringExtensionMethods.cs @@ -54,14 +54,17 @@ namespace TUGraz.VectoCommon.Utils throw new FormatException("Cannot convert an empty string to a number."); } - try { - return double.Parse(self, CultureInfo.InvariantCulture); - } catch (FormatException) { - if (defaultValue.HasValue) { - return defaultValue.Value; - } - throw; + var success = double.TryParse(self, out var retVal); + if (success) { + return retVal; + } + + if (defaultValue.HasValue) { + return defaultValue.Value; } + + // throws an exception + return double.Parse(self, CultureInfo.InvariantCulture); } public static int ToInt(this string self, int? defaultValue = null)