/// <summary> /// Try parsing a correctly-formatted float floating-point literal into the nearest representable float /// using IEEE round-to-nearest-ties-to-even rounding mode. Behavior is not defined for inputs that are /// not valid C# floating-point literals. /// </summary> /// <param name="s">The float floating-point constant's string</param> /// <param name="f">The nearest float value, if conversion succeeds</param> /// <returns>True if the input was converted; false if there was an overflow</returns> public static bool TryParseFloat(string s, out float f) { DecimalFloatingPointString str = DecimalFloatingPointString.FromSource(s); FloatFloatingPointType dbl = FloatFloatingPointType.Instance; Status status = RealParser.ConvertDecimalToFloatingPointBits(str, dbl, out ulong result); f = Int32BitsToFloat((uint)result); return(status != Status.Overflow); }
/// <summary> /// Try parsing a correctly-formatted double floating-point literal into the nearest representable double /// using IEEE round-to-nearest-ties-to-even rounding mode. Behavior is not defined for inputs that are /// not valid C# floating-point literals. /// </summary> /// <param name="s">The decimal floating-point constant's string</param> /// <param name="d">The nearest double value, if conversion succeeds</param> /// <returns>True if the input was converted; false if there was an overflow</returns> public static bool TryParseDouble(string s, out double d) { DecimalFloatingPointString str = DecimalFloatingPointString.FromSource(s); DoubleFloatingPointType dbl = DoubleFloatingPointType.Instance; Status status = RealParser.ConvertDecimalToFloatingPointBits(str, dbl, out ulong result); d = BitConverter.Int64BitsToDouble((long)result); return(status != Status.Overflow); }
/// <summary> /// Try parsing a correctly-formatted float floating-point literal into the nearest representable float /// using IEEE round-to-nearest-ties-to-even rounding mode. Behavior is not defined for inputs that are /// not valid C# floating-point literals. /// </summary> /// <param name="s">The float floating-point constant's string</param> /// <param name="f">The nearest float value, if conversion succeeds</param> /// <returns>True of the input was converted; false if there was an overflow</returns> public static bool TryParseFloat(string s, out float f) { try { var str = DecimalFloatingPointString.FromSource(s); var dbl = FloatFloatingPointType.Instance; ulong result; var status = RealParser.ConvertDecimalToFloatingPointBits(str, dbl, out result); f = Int32BitsToFloat((uint)result); return(status != Status.Overflow); } catch (System.FormatException) { f = 0.0f; return(false); } }
/// <summary> /// Try parsing a correctly-formatted double floating-point literal into the nearest representable double /// using IEEE round-to-nearest-ties-to-even rounding mode. Behavior is not defined for inputs that are /// not valid C# floating-point literals. /// </summary> /// <param name="s">The decimal floating-point constant's string</param> /// <param name="d">The nearest double value, if conversion succeeds</param> /// <returns>True of the input was converted; false if there was an overflow</returns> public static bool TryParseDouble(string s, out double d) { try { var str = DecimalFloatingPointString.FromSource(s); var dbl = DoubleFloatingPointType.Instance; ulong result; var status = RealParser.ConvertDecimalToFloatingPointBits(str, dbl, out result); d = BitConverter.Int64BitsToDouble((long)result); return(status != Status.Overflow); } catch (System.FormatException) { // this can occur when the exponent is empty (e.g. "0.0e") or too large to fit in an integer d = 0.0; return(false); } }