Do you want to support the names of the current culture, so if it's "de-DE" "+unendlich"
should be detected successfully as PositiveInfinitySymbol
? It's not clear whether the input is controlled by you or not.
You:
If the user's CurrentCulture
is "de-DE", I would like "+unendlich"
(which is PositiveInfinitySymbol
) to parse successfully as
double.PositiveInfinity
. I would also like "+Unendlich" and
"UNENDLICH" to parse to the same value.
Then your approach absolutely fine, isn't it? You could write a method like this:
public static bool ParseDoubleExt(string input, out double doubleVal, StringComparison comparison = StringComparison.CurrentCultureIgnoreCase, NumberFormatInfo nfi = null)
{
if (nfi == null)
nfi = NumberFormatInfo.CurrentInfo;
doubleVal = double.MinValue;
double d;
if (double.TryParse(input, out d))
{
doubleVal = d;
return true;
}
else
{
bool isPosInf = nfi.PositiveInfinitySymbol.Equals(input, comparison);
if (isPosInf)
{
d = double.PositiveInfinity;
return true;
}
bool isNegInf = nfi.NegativeInfinitySymbol.Equals(input, comparison);
if (isNegInf)
{
d = double.NegativeInfinity;
return true;
}
bool isNAN = nfi.NaNSymbol.Equals(input, comparison);
if (isNAN)
{
d = double.NaN;
return true;
}
// to be extended ...
}
return false;
}
and use it in this way:
string doubleStr = "+UNENDLICH";
double d;
bool success = ParseDoubleExt(doubleStr, out d);