/// <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) { 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); }
/// <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) { 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); }