Пример #1
0
        /// <summary>
        /// Converts a single precision floating point number to a string.
        /// </summary>
        /// <param name="result">The location where the result will be placed.</param>
        /// <param name="value">The value to format.</param>
        /// <param name="precision">The precision to output when formatting.</param>
        /// <param name="options">The format mode to use.</param>
        /// <param name="provider">The provider used to determine how the value will be
        /// formatted.</param>
        public static void ToString(StringBuilder result, float value, int precision,
                                    RyuFormatOptions options = RyuFormatOptions.RoundtripMode,
                                    IFormatProvider provider = null)
        {
            var info = RyuUtils.GetFormatInfo(provider);
            // Step 1: Decode the floating-point number, and unify normalized and subnormal
            // cases
            bool ieeeSign = RyuFloat32.Decode(value, out uint ieeeMantissa,
                                              out uint ieeeExponent);

            // Case distinction; exit early for the easy cases
            bool mZero = ieeeMantissa == 0UL;

            if (ieeeExponent == RyuFloat32.EXPONENT_MASK)
            {
                RyuUtils.GenerateSpecial(result, ieeeSign, !mZero, info);
            }
            else if (mZero && ieeeExponent == 0UL)
            {
                RyuUtils.GenerateZero(result, ieeeSign, precision, options, info);
            }
            else if ((options & RyuFormatOptions.RoundtripMode) != 0)
            {
                new RyuFloat32(ieeeSign, ieeeMantissa, ieeeExponent).ToRoundtripString(result,
                                                                                       options, info);
            }
        }